0

I have a one to many relationship between Project and Insurance. When a record is created on Insurance object, it needs to get the Project record and then count how many Insurance records are present and it needs to input the value in the Name field of the Insurance record.

Issue is that when I insert 500 records, it works fine but if I insert 1000 records I am getting a CPU Timeout exception. What could be the cause of it?

Trigger

trigger InsuranceTrigger on Insurance__c (before insert) {
        
    SWITCH ON Trigger.operationType {
        WHEN BEFORE_INSERT{
            InsuranceClass.insuranceAutomation(Trigger.new);
        }
    }
}

Apex Class:

public class InsuranceClass {
    public static void insuranceAutomation(List<Insurance__c> insuranceList){
        Set<Id> insuranceIds = new Set<Id>();
        
        //Get all the project Id for the corresponding new insurance records
        for(Insurance__c i : insuranceList){
            insuranceIds.add(i.Project__c);
            system.debug(insuranceIds);
        }

        //Query the number of insurance records for the corresponding projects
        Map<Id,Milestone1_Project__c> is = new Map<Id,Milestone1_Project__c>([SELECT Id, Cleantech_Reference__c, 
                                                                              (SELECT Id, Project__c, Name FROM Insurance__r) 
                                                                              FROM Milestone1_Project__c WHERE Id in :insuranceIds]);
        
        Map<Id, Integer> newMap = new Map<Id, Integer>();        
        for(Insurance__c i : insuranceList){
            
            //if there is no insurance record associated with the project and if the newMap is empty
            if(is.get(i.Project__c).Insurance__r.isEmpty() && !newMap.containskey(i.Project__c)){
                Integer count = 1;
                i.Name = is.get(i.Project__c).Cleantech_Reference__c+'/'+'INS-'+string.valueOf(count);
                newMap.put(i.Project__c, count);
                
            }
            //if there are insurance records and the newMap is empty
            else if(!is.get(i.Project__c).Insurance__r.isEmpty()&& !newMap.containskey(i.Project__c) )
            {
                Integer count = is.get(i.Project__c).Insurance__r.size()+1;
                i.Name = is.get(i.Project__c).Cleantech_Reference__c+'/'+'INS-'+string.valueOf(count);
                newMap.put(i.Project__c, count);
            }
            //if the newMap is not empty
            else{
                Integer count = newMap.get(i.Project__c)+1;
                i.Name = is.get(i.Project__c).Cleantech_Reference__c+'/'+'INS-'+string.valueOf(count);
                newMap.put(i.Project__c, count);
                
            }
        }   
    }
}

Execute Anonymous Window

List<Insurance__c> insList = new List<Insurance__c>();
for(Integer i=0;i<1500;i++){
Insurance__c is = new Insurance__c();
is.Project__c = 'a03O000000RsnCZIAZ';
insList.add(is);
}
insert insList;

1 Answer 1

0

For the functionality that you need I would highly recommend installing declarative-lookup-rollup-summaries. It is a free tool that I have been using for 2 years and it has saved numerous hours of work.

https://www.youtube.com/watch?v=5WWbmleGlVk

https://github.com/afawcett/declarative-lookup-rollup-summaries

6
  • Hi Boris, thank you but I’m not interested in procuring the tool now. Would appreciate if you could suggest me ideas to fix the issue. Feb 17, 2021 at 8:47
  • You are already following the good practices in your trigger so I do not see how you can optimize it. That is why I suggest you move this functionality from BEFORE_INSERT to scheduled apex and there leverage the great SF batching framework: trailhead.salesforce.com/content/learn/modules/… Feb 17, 2021 at 9:07
  • Also you can check this so that you do not trigger the trigger on data imports. developer.salesforce.com/forums/?id=906F0000000AXdDIAW Feb 17, 2021 at 9:08
  • The CPy time limit in triggers is there for a reason and it is only for light operations Feb 17, 2021 at 9:10
  • you can also try to change the trigger to AFTER_INSERT (not sure if this will actually help, but I am sure that you can do more stuff in after triggers and this will not mess with the actual saving of the record). And last you can try with triggered flow on AFTER_INSERT. Feb 17, 2021 at 9:14

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.