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
Kaylee GrayKaylee Gray 

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.

This is for the Apex Specialist Challenge 4. All my tests pass and code coverage is 100%.
I'm getting the error when I check the challenge.
In the debug logs they are checking the following " SOQL_EXECUTE_BEGIN [35]|Aggregations:0|SELECT COUNT() FROM Case WHERE Equipment__c = :tmpVar1" which is giving me  Assertion Failed: Expected: 402, Actual: 602. 

I've read through the log and just can't figure out what's going wrong.

Helper Code:
public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(List<case>ClosedCaseList){
        // update workorders
        
        list<case> insertCaseList = new list<case>();
        
        for(Case c : ClosedCaseList)
            
            {
                
                Case newCase = new Case();
                integer days = (integer)c.Equipment__r.Maintenance_Cycle__c;
                newCase.Type = 'Routine Maintenance';
                newCase.Status = 'New';
                newCase.Subject =  c.Subject;
                newCase.Date_Reported__c = Date.today();
                if(days != null){
                newCase.Vehicle__c = c.Vehicle__c;
                newCase.Date_Due__c = Date.today().addDays(days);
                newCase.Equipment__c = c.Equipment__c;
                }
                
                insertCaseList.add(newCase);
            }
        
        if(insertCaseList.size()>0){
        insert insertCaseList;
        }
    }
        
}

Trigger:

trigger MaintenanceRequest on Case ( after update) {
    
     List<case>ClosedCaseList = [SELECT Id, Type, Subject, Vehicle__c, Equipment__c, Equipment__r.Maintenance_Cycle__c FROM Case WHERE status = 'Closed'];
  
     List<Case> casesToUpdate = new List<case>();
    
    if(Trigger.isAfter && Trigger.isUpdate){
     for(case c : ClosedCaseList){
        
        if(c.type == 'Repair' || c.type =='Routine Maintenance'){
            
            casesToUpdate.add(c);
        }
    }
    }
    MaintenanceRequestHelper.updateWorkOrders(casesToUpdate);
}


Thanks in advance for any help.
SandhyaSandhya (Salesforce Developers) 
Hi,

Please refer below links for a similar issue.

https://developer.salesforce.com/forums/?id=906F0000000kFjcIAE
 
http://salesforce.stackexchange.com/questions/130242/superbadges-apex-specialist-the-maintenancerequest-trigger-does-not-appear
 
https://d2hwpln2xvs3s.cloudfront.net/forums/?id=906F0000000kEG5IAM
 
Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya

 
Kaylee GrayKaylee Gray
Hi Sandhya, Thank you for taking time to respond. Unfortunately I reviewed these links several times before making my post, but they don't quite help me with my particular issue. Regards, Kaylee
Prem Kumar 70Prem Kumar 70
You have to insert as many work parts records for each inserted new case record.

Here's 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;
    }
}