function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sudhir Kumar 19Sudhir Kumar 19 

Apex Superbadge Challenge 4 Error: The 'MaintenanceRequest' trigger does not appear to be handling bulk operations correctly.

Apex Superbadge challenge 4 error

Apex Superbadge Challenge 4 Error

Challenge Not yet complete... here's what's wrong: 
The 'MaintenanceRequest' trigger does not appear to be handling bulk operations correctly. For the positive use case of inserting and updating more than 200 records, it did not produce the expected outcome.

When I debug, I see this strange SOQL:

11:07:41.744 (4865812076)|SOQL_EXECUTE_BEGIN|[35]|Aggregations:0|SELECT COUNT() FROM Case WHERE subject LIKE '%AMC Spirit%'
11:07:41.744 (4868187164)|SOQL_EXECUTE_END|[35]|Rows:201
11:07:41.744 (4868199387)|HEAP_ALLOCATE|[35]|Bytes:4
11:07:41.744 (4868273203)|EXCEPTION_THROWN|[35]|System.AssertException: Assertion Failed: Expected: 402, Actual: 201
11:07:41.744 (4868404432)|HEAP_ALLOCATE|[35]|Bytes:48
11:07:41.744 (4868515907)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: 402, Actual: 201

Debug log

I see many people passing this challenge. Kindly help!
parth jollyparth jolly
Hi ,

Please this link it might help you out.

http://salesforce.stackexchange.com/questions/130242/superbadges-apex-specialist-the-maintenancerequest-trigger-does-not-appear

Thanks

Paarth Jolly
Sudhir Kumar 19Sudhir Kumar 19
@parth 
This doens't seem working. No clues how to assert trailhead validation.
Mike CrosbyMike Crosby
I just came across this issue as well.  In my trigger helper, I made the Subject for new Cases not null by setting it to a simple value of "Scheduled Maintenance".  After troubleshooting and reviewing the logs, it appears that this requirement is misstated:

" When an existing maintenance request of type Repair or Routine Maintenance is Closed, you create a new maintenance request for a future routine checkup. This new request is tied to the same vehicle and piece of equipment to be serviced as the original closed service request. This new request's Type should be set as Routine Maintenance. The Subject should not be null and the Report Date field reflects the day the request was created. Another aspect about parts is they all have different lifespans. Therefore, you need to calculate and set the next due date using the maintenance cycle defined on the related work part records. If multiple work parts are used in the maintenance request, choose the shortest maintenance cycle to define the service date."

When I reviewed the log, I noticed that the challenge test code is querying for all Case records that have a similar Subject value to verify that new Case records are created: 

SELECT COUNT() FROM Case WHERE subject LIKE '%AMC Spirit%'

After seeing this, I set the Subject of the new Case records = to the Subject of the Case that is being closed and was able to complete the 4th challenge.

Hope that helps.
 
Prem Kumar 70Prem Kumar 70

You have to insert as many work parts records for each inserted new case record.
Here is my code for helper class:

public class MaintenanceRequestHelper {
    public static void updateWorkOrders(Map<Id,Case> cOldMap, Map<Id,Case> cNewMap){
        List<Id> caseToProcessIdList = new List<Id>();
        Map<Id,List<Work_Part__c>> caseIdToWpMap = new Map<Id,List<Work_Part__c>>();
        Map<Integer,Case> insertCaseMap = new Map<Integer,Case>();
        Map<Integer,List<Work_Part__c>> insertWpMap = new Map<Integer,List<Work_Part__c>>();
        List<Work_Part__c> insertAllWpList = new List<Work_Part__c>();
        Integer commonKey = 0;
        for(Id csId : cNewMap.keySet()){
            if(cOldMap.get(csId).status != 'Closed' && cNewMap.get(csId).status == 'Closed' && (cNewMap.get(csId).type == 'Repair' || cNewMap.get(csId).type == 'Routine Maintenance'))
                caseToProcessIdList.add(csId);
        }
        if(caseToProcessIdList.size() == 0)
            return;
        for (Work_Part__c wp: [Select Id, Name, Equipment__c, Equipment__r.Maintenance_Cycle__c, Maintenance_Request__c, Quantity__c From Work_Part__c Where Maintenance_Request__c IN :caseToProcessIdList]) {
            if(!caseIdToWpMap.containsKey(wp.Maintenance_Request__c))
                caseIdToWpMap.put(wp.Maintenance_Request__c, new List<Work_Part__c>{wp});
            else{
                List<Work_Part__c> tempList = caseIdToWpMap.get(wp.Maintenance_Request__c);
                tempList.add(wp);
                caseIdToWpMap.put(wp.Maintenance_Request__c, tempList);
            }
        }
        for(Id csId : caseToProcessIdList){
            List<Work_Part__c> relatedWpList = caseIdToWpMap.get(csId);
            List<Work_Part__c> insertWpList = new List<Work_Part__c>();
            Integer minDays;
            if(relatedWpList==null || relatedWpList.size()==0){
                relatedWpList = new List<Work_Part__c>();
                minDays = 0;
            }            
            for(Work_Part__c wp : relatedWpList){
                if(minDays == null || minDays >= wp.Equipment__r.Maintenance_Cycle__c.intValue())
                    minDays = wp.Equipment__r.Maintenance_Cycle__c.intValue();
                insertWpList.add(new Work_Part__c(Equipment__c=wp.Equipment__c, Quantity__c=wp.Quantity__c));
            }
            Case oldCase = cNewMap.get(csId);
            Case cse = new Case();
            cse.Type ='Routine Maintenance';
            cse.Status ='New';
            cse.Origin =oldCase.Origin;
            cse.Vehicle__c = oldCase.Vehicle__c;
            cse.Equipment__c = oldCase.Equipment__c;
            cse.Subject = String.isBlank(oldCase.Subject) ? 'subject' : oldCase.Subject;
            cse.Date_Reported__c = System.today();
            cse.Date_Due__c = System.Today().addDays(minDays);
            insertCaseMap.put(commonKey, cse);
            insertWpMap.put(commonKey, insertWpList);
            commonKey++;
        }
        insert insertCaseMap.values();
        for(Integer i : insertCaseMap.keySet()){
            for(Work_Part__c wp : insertWpMap.get(i)){
                wp.Maintenance_Request__c = insertCaseMap.get(i).Id;
                insertAllWpList.add(wp);
            }
        }
        insert insertAllWpList;
    }
}
Deeksha PurohitDeeksha Purohit
can someone help me with this class. I am trying to write a test class but only managed to get 70% i need 100% to pass the challenge. plxx help me with the code


public with sharing class MaintenanceRequestHelper {
    public static void updateWorkOrders(List<Case> caseList) {
        List<case> newCases = new List<Case>();
        Map<String,Integer> result=getDueDate(caseList);
        list<Equipment_Maintenance_Item__c> itemsListToinsert= new list<Equipment_Maintenance_Item__c>();
      Map<String,list<Equipment_Maintenance_Item__c>> resultItmes=getMaintainanceItems(caseList);
        for(Case c : caseList){
            if(c.status=='closed')
                if(c.type=='Repair' || c.type=='Routine Maintenance'){
                    Case newCase = new Case();
                    newCase.Status='New';
                    newCase.Origin='web';
                    newCase.Type='Routine Maintenance';
                    newCase.Subject='Routine Maintenance of Vehicle';
                    newCase.Vehicle__c=c.Vehicle__c;
                    newCase.Equipment1__c=c.Equipment1__c;
                    newCase.Date_Reported__c=Date.today();
                    newcase.Cloned_Case_Id__c=c.Id;
                    if(result.get(c.Id)!=null)
                     newCase.Date_Due__c=Date.today()+result.get(c.Id);
                    else
                        newCase.Date_Due__c=Date.today();
                    newCases.add(newCase);
                }
        }        
       if(newCases.size()>0)
       {      
           insert newCases; 
           
           for(Case c : newCases){
             List<Equipment_Maintenance_Item__c> temp =  resultItmes.get(c.Cloned_Case_Id__c);
               if(temp !=null){
                    for(Equipment_Maintenance_Item__c row:temp){
                    Equipment_Maintenance_Item__c newitem = new Equipment_Maintenance_Item__c();
                        newitem.Equipment__c=row.Equipment__c;
                        newitem.Maintenance_Request__c= c.Id;
                        newitem.Quantity__c= row.Quantity__c;  
                        itemsListToinsert.add(newitem);
                        
                    }
               }
               
           }
           
           
       }
        
       if(itemsListToinsert.size()>0)
           insert itemsListToinsert;
    }
    //
    public static  Map<String,Integer> getDueDate(List<case> CaseIDs){       
       Map<String,Integer> result = new Map<String,Integer>();
        Map<Id, case> caseKeys = new Map<Id, case> (CaseIDs);        
       List<AggregateResult> wpc=[select Maintenance_Request__r.ID cID,min(Equipment__r.Maintenance_Cycle__c)cycle
                      from Equipment_Maintenance_Item__c where  Maintenance_Request__r.ID in :caseKeys.keySet() and Equipment__r.Maintenance_Cycle__c != null group by             Maintenance_Request__r.ID ];
        for(AggregateResult res :wpc){
            Integer addDays=0;
            if(res.get('cycle')!=null)
                addDays+=Integer.valueOf(res.get('cycle'));
            result.put((String)res.get('cID'),addDays);
        }
        return result;
}
    
    
     public static Map<String,list<Equipment_Maintenance_Item__c>> getMaintainanceItems(List<case> CaseIDs){       
     
       Map<String,list<Equipment_Maintenance_Item__c>> mapofMaintainanceItems = new Map<String,list<Equipment_Maintenance_Item__c>>();
        Map<Id, case> caseKeys = new Map<Id, case> (CaseIDs);        
       list<Equipment_Maintenance_Item__c> lstrec = new  list<Equipment_Maintenance_Item__c>([select id ,Maintenance_Request__c,Equipment__c,Quantity__c  
                      from Equipment_Maintenance_Item__c where  Maintenance_Request__r.ID in :caseKeys.keySet() ]);
        
         for(Equipment_Maintenance_Item__c row:lstrec){
              
                if (mapofMaintainanceItems.containsKey(row.Maintenance_Request__c)) {
                    List<Equipment_Maintenance_Item__c> temp = mapofMaintainanceItems.get(row.Maintenance_Request__c);
                        temp.add(row);
                    mapofMaintainanceItems.put(row.Maintenance_Request__c, temp);
                }
                else{
                    mapofMaintainanceItems.put(row.Maintenance_Request__c, new List<Equipment_Maintenance_Item__c> { row });
                }    
                       
         }
         
        return mapofMaintainanceItems;
}
    
    
}