0

I would like to understand the difference between snippet 1 and snippet 2. In snippet 1 I get a DML rows limit exception whereas in snippet 2, I don't get any exception. What makes snippet 2 not throw any error over snippet 1? Snippet 1 uses two for loops (newCase and eMLlist) and both the for loops uses maps to fetch the values. Snippet 2 on the other end uses a list casetoInsert and a map closedCasesM.get(caseLoop.ParentId).Equipment_Maintenance_Items__r. I am puzzled as to what makes snippet 2 efficient. Appreciate if you could highlight the difference.

Snippet 1:

 for(Case cd : newCase.values()){
            system.debug('cd.Id'+cd.Id);
            for(Equipment_Maintenance_Item__c e : eMLlist.values()){
                Equipment_Maintenance_Item__c emi = new Equipment_Maintenance_Item__c();
                
                emi.Quantity__c = e.Quantity__c;
                emi.Equipment__c = e.Equipment__c;
                emi.Maintenance_Request__c = cd.Id;
                emiList.add(emi);
            }}
        
        insert emiList;

Snippet 2:

 Map<Id,Case> closedCasesM = new Map<Id,Case>([SELECT Id, (SELECT Id, Equipment__c, Quantity__c FROM Equipment_Maintenance_Items__r) FROM Case WHERE Id IN :caseMap]);
        
        system.debug('newcasethatisinserted'+ closedCasesM);
        for(Case caseLoop : casetoInsert){
        for(Equipment_Maintenance_Item__c e : closedCasesM.get(caseLoop.ParentId).Equipment_Maintenance_Items__r){
            Equipment_Maintenance_Item__c emi = new Equipment_Maintenance_Item__c();
            emi.Quantity__c = e.Quantity__c;
            emi.Equipment__c = e.Equipment__c;
            emi.Maintenance_Request__c = caseLoop.Id;
            
            emiList.add(emi);
        }
        }
        insert emiList;
3

While neither code snippet is a complete example, the key distinguishing factor appears to be these lines:

for(Equipment_Maintenance_Item__c e : eMLlist.values()){
        

versus

for(Equipment_Maintenance_Item__c e : closedCasesM.get(caseLoop.ParentId).Equipment_Maintenance_Items__r){

In the first version, your inner for loop iterates over all Equipment_Maintenance_Item__c records for every Case (the outer loop), whether the Equipment_Maintenance_Item__c record is related to the current Case or not. This results in multiplicative creation of irrelevant records at high volume - count(Case) * count(Equipment_Maintenance_Item__c).

In the second version, you appear to be correctly filtering to iterate over, in the inner loop, only the Equipment_Maintenance_Item__c records related to the current Case, which results, apparently, in creating only the desired records - only count(Equipment_Maintenance_Item__c).

So in fact, it's not an efficiency concern here so much as a behavior problem - the code as implemented is not behaving correctly - that ultimately manifests a limits issue.

2
  • Huh. So I'm not the only one who checks in late Sunday night 😅. – sfdcfox Feb 8 at 3:47
  • Thank you David. – Krishnamurthy Feb 8 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.