0

I'm Working through this challenge and appear to have run into an issue i have not found a solution to (nor does it appear that anyone has posted a question on this specifically.)

looking at the log is pretty painful as any System.debug() lines appear to be dropped due to the size of the log being >18MB but it appears there are 40000 rows to be added. Having caved and looked at some of the other code dumps out there i dont see that anyone else looks to have had this issue or made any special arrangements with batching and as far as i can see i haven't done anything drastically different to them (but the subtlety is clearly more than enough to cause an issue).

So if anyone could have a look over this and give me a hint as to where/why i have gone wrong it would be a great help. I'm not looking for a link to a working code set to copy i'm hoping for some understanding as to where my approach is incorrect/causing issues

With my trigger

trigger MaintenanceRequest on Case ( after update) {    

List<Case> closedCases = new List<Case>();
for (Case c : Trigger.New){

    System.debug('Type :' + c.Type );
    System.debug('Equals Repair: ' +c.Type.equalsIgnoreCase('Repair'));
    System.debug('Equals Routine Maintenance: ' +c.Type.equalsIgnoreCase('Routine Maintenance'));

    if (
        (c.Type.equalsIgnoreCase('Repair') || c.Type.equalsIgnoreCase('Routine Maintenance')) 
        && c.isClosed == true )
        {
        closedCases.add(c);
    }
}
System.debug(closedCases.size() + ' cases were closed');

if(closedCases.size() > 0){
    MaintenanceRequestHelper.createScheduledMaintainance(closedCases);
}

}

and helper

public with sharing class MaintenanceRequestHelper {
public MaintenanceRequestHelper() {

}


public static Map<Id,Integer> getMainainanceCycleForCases(){
    List<AggregateResult> maintainTimes = [SELECT Maintenance_Request__c, MIN(Equipment__r.Maintenance_Cycle__c)cycle
                                            FROM Equipment_Maintenance_Item__c
                                            GROUP BY Maintenance_Request__c 
                                            ];
    System.debug(maintainTimes);
    Map<Id,Integer> maintinTimeMap = new Map<Id,Integer>();

    for(   AggregateResult  e :  maintainTimes ){
        maintinTimeMap.put((Id)e.get('Maintenance_Request__c'), ((Double)e.get('cycle')).intValue());
    }

    System.debug(maintinTimeMap);

    return maintinTimeMap;
}

public static Map<Id,List<Id>> getMaintainanceItems(List<Case> closedCases){
    // map maintainance_Request_ID, Equipment_Maintenance_Item__c
    List<Equipment_Maintenance_Item__c> equipmentMaintainanceList = [SELECT Maintenance_Request__c,
                                                    Equipment__r.Id 
                                            FROM Equipment_Maintenance_Item__c
                                            Where Maintenance_Request__c IN :closedCases];


    Map<Id,List<Id>> maintainance_equipmentMap = new Map<Id, List<Id>>();

    for (Equipment_Maintenance_Item__c e:equipmentMaintainanceList){
        System.debug('Putting : '+ e.Maintenance_Request__c + ' : ' + e.Equipment__r.Id);
        if(maintainance_equipmentMap.containsKey(e.Maintenance_Request__c)){
            maintainance_equipmentMap.get(e.Maintenance_Request__c).add(e.Equipment__r.Id);
        }else{
            maintainance_equipmentMap.put(e.Maintenance_Request__c,new List<id>{e.Equipment__r.Id});
        }
        System.debug('Map now : '+ maintainance_equipmentMap);
    }

    System.debug('Returning: ' +maintainance_equipmentMap);

    return maintainance_equipmentMap;
}



public static void createScheduledMaintainance(List<Case> closedCaseList) {
    System.debug(closedCaseList.size() + ' Cases to create');

    Map<Id,Integer> maintainTimeMap = getMainainanceCycleForCases();
    Map<Id,List<Id>> maintainanceItems = getMaintainanceItems(closedCaseList);   // map maintainance_Request_ID, Equipment_Maintenance_Item__c

    List<Case> createNewCases = new List<Case>();
    
    for(Case c : closedCaseList){
        Case newCase = new Case();
        newCase.Type = 'Routine Maintenance';
        newCase.Status = 'New';
        newCase.Vehicle__c = c.Vehicle__c;
        newCase.Subject = c.Subject;
        newCase.Date_Reported__c = Date.today();
        newCase.Date_Due__c = Date.today() + maintainTimeMap.get(c.Id);
        newCase.ProductId = c.ProductId;

        createNewCases.add(newCase);
    }

    System.debug(createNewCases.size() +' to insert');
    list<Equipment_Maintenance_Item__c> itemsListToinsert= new list<Equipment_Maintenance_Item__c>();

    if(createNewCases.size()>0){
        insert createNewCases;
  

        for(Case ca : createNewCases){
            
        
            for(Id key: maintainanceItems.keySet()){
                List<Id> equipment = maintainanceItems.get(key);
                for (Id e : equipment){
                    Equipment_Maintenance_Item__c newitem = new Equipment_Maintenance_Item__c();
                        newitem.Equipment__c=e;
                        newitem.Maintenance_Request__c= ca.Id;
                        itemsListToinsert.add(newitem);
                }
            }
        
        }

    }

    System.debug('itemsListToinsert Size: ' +itemsListToinsert.size());
    if (itemsListToinsert.size() >0){
        insert itemsListToinsert; //<<<< ERROR TRIGGERS HERE and has ~40000 rows <<<<<
    }

}

}

1 Answer 1

0

So, It seems i was over complicating the issue to an extent.

Having taken a break and walking through the problem again something just didnt sit right with this block of code.

if(createNewCases.size()>0){
    insert createNewCases;


    for(Case ca : createNewCases){
        
    
        for(Id key: maintainanceItems.keySet()){
            List<Id> equipment = maintainanceItems.get(key);
            for (Id e : equipment){
                Equipment_Maintenance_Item__c newitem = new Equipment_Maintenance_Item__c();
                    newitem.Equipment__c=e;
                    newitem.Maintenance_Request__c= ca.Id;
                    itemsListToinsert.add(newitem);
            }
        }
    
    }

}

For -> For -> For -> create new Equipment_Maintenance_Item__c

With 0 checks to see if this new Equipment_Maintenance_Item__c was needed.

I ended up partly re-writing the class but the main change was re-working the section of code above to

List<Equipment_Maintenance_Item__c> equipmentMaintainanceList = [SELECT Maintenance_Request__c,
                                                                            Equipment__r.Id 
                                                                    FROM Equipment_Maintenance_Item__c
                                                                    Where Maintenance_Request__c IN :closedCaseList];


        List<Equipment_Maintenance_Item__c> updatedEquipment = new List<Equipment_Maintenance_Item__c>();
        for(Case closed : closedCaseList){
            for(Equipment_Maintenance_Item__c eqip : equipmentMaintainanceList){

                if (eqip.Maintenance_Request__c == closed.Id){
                    Equipment_Maintenance_Item__c e = eqip.clone(false, false, false, false);
                    e.Maintenance_Request__c = oldToNewMap.get(closed.Id).Id;
                    updatedEquipment.add(e);
                }
            }
        }

        if (updatedEquipment.size() > 0 ){
            insert updatedEquipment;
        }

For me the lesson is be very mindful of nested for loops when the outcome is going to come up against some hard and fast governor limits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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