0

Getting CPU time limit exceeded issue in trigger level.

Trigger code:

    list<depot__c> depotList = new list<depot__c>();
for(Depot__c depot : Trigger.new){
    depotList.add(depot);
}

Map<Id, Asset> mapOfAsset = new Map<Id, Asset>([select id, some other required fields from Asset where (assetDepot__c IN: depotList) and (SLA__c like '%4%')]);
if(mapOfAsset.size() > 0){

for(depot__c depotsasset : depotList){
    for(Asset assetid : mapOfAsset.values())
    {
        if(depotsasset.Id == assetid.assetDepot__c){
            assetid.assetNearbyDepot__c = null;  
            assetUpdate.add(assetid);
        }
    }
}

Map<Id, Object2__c> mapOfObject2 = new Map<Id, Object2__c>([select id, some required fields from Object2__c where obDepot__c IN: depotList]);
if(mapOfObject2.size()>0){
    for(Object2__c addressid : mapOfObject2.values()){
        addressid.obDepot__c = null;
        addressUpdate.add(addressid);
    }

}
Map<id, Object3__c> mapOfObject3 = new Map<id, RMAv2__c>([select id, some required fields from RMAv2__c where (object3Depot__c IN: depotList) and (object3Sla__c like '%4%') and (object3Status__c = 'Draft')]);
if(mapOfObject3.size()>0){
    for(RMAv2__c rmaid : mapOfRMA.values()){
        rmaid.object3Depot__c = null;
        rmaUpdate.add(rmaid);
    }
}
if(assetUpdate.size()>0){
    update assetUpdate; //nearly 600 records
}
if(addressUpdate.size() > 0){
    update addressUpdate; //nearly 500 records
}
if(object3Update.size() > 0){
    update object3Update; //50 records
}

Nearly updating 1150 records across 3 objects. The code is already using map query to optimize. Is there way out to optimize more?.

3
  • There isn't much we could change in this piece of code to improve Apex CPU time. You'll have to revisit the three triggers in context.
    – Avinash
    Oct 17, 2017 at 11:15
  • Avoid nested for loops, In the given code they seem to be one of the culprit.
    – Raul
    Oct 17, 2017 at 11:22
  • But in that loop, the list variable 'depotList' returns/contains only one record Oct 17, 2017 at 11:35

1 Answer 1

2

There are a few things that could affect the performance. For example this loop:

for(Depot__c depot : Trigger.new){
    depotList.add(depot);
}

Just remove it and depotList variable, instead you can just use Trigger.new instead.

Then, try to optimize your queries:

Map<Id, Asset> mapOfAsset = new Map<Id, Asset>([select id, some other required fields from Asset where (assetDepot__c IN: depotList) and (SLA__c like '%4%')]);
Map<id, Object3__c> mapOfObject3 = new Map<id, RMAv2__c>([select id, some required fields from RMAv2__c where (object3Depot__c IN: depotList) and (object3Sla__c like '%4%') and (object3Status__c = 'Draft')]);

Using LIKE '%4%' is very heavy for performance. This will force the query to scan every single character in this field and try to find 4

And also try to get rid of the nested loop:

for(depot__c depotsasset : depotList){
    for(Asset assetid : mapOfAsset.values())
    {
        if(depotsasset.Id == assetid.assetDepot__c){
            assetid.assetNearbyDepot__c = null;  
            assetUpdate.add(assetid);
        }
    }
}

I think you may be able to re-write it like this:

for (Asset assetid : mapOfAsset.values()) {
    if (Trigger.newMap.containsKey(assetid.assetDepot__c)) {
        assetid.assetNearbyDepot__c = null;  
        assetUpdate.add(assetid);
    }
}
1
  • 2
    SOQL execution time is not counted as part of Apex CPU time limit.
    – Avinash
    Oct 17, 2017 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .