+ Start a Discussion
Vinothini Murugesh 23Vinothini Murugesh 23 

CPU Time Limit exceeded error for my code.please help me to solve this

Trigger:

trigger UpdateRelIntConIds5 on Contract_Group_Association__c (after delete, after insert, after update) {
    Set<Id> accounts = new Set<Id>();
Map<id,id> contractgroupid=new Map<id,id>();
    Set<Id> cons = new Set<Id>();
     Set<Id> contracts = new Set<Id>();
system.debug('old'+Trigger.old );
    if (Trigger.old != null) {
        system.debug('inside update');
        for (Contract_Group_Association__c c : Trigger.old) {
            if (c.Internal_Contract_Group__c != null)
                cons.add(c.Internal_Contract_Group__c);
        }
    }


    if (Trigger.new != null) {
        system.debug('inside insert');
        for (Contract_Group_Association__c c : Trigger.new) {
            if (c.Internal_Contract_Group__c != null)
                cons.add(c.Internal_Contract_Group__c);
        }
    }
   if (cons.size()> 0){
    List<Customer_Groups_Association__c> conGroup = [select Internal_Contact_Group__c,Internal_Contract_Group__c from Customer_Groups_Association__c where Internal_Contract_Group__c in :cons];
     system.debug('conGroup'+conGroup.size());
    
    for (Customer_Groups_Association__c cx : conGroup) {
        accounts.add(cx.Internal_Contact_Group__c);
      contractgroupid.put(cx.id,cx.Internal_Contract_Group__c); 
     
          system.debug('cx.Internal_Contact_Group__c: '+cx.Internal_Contact_Group__c);
    }  
    }
   system.debug('Groups'+accounts);
    UpdateRelatedInternalContractIds ur = new UpdateRelatedInternalContractIds(accounts,contractgroupid);
    ur.go();
   
}

apex class:

global class UpdateRelatedInternalContractIds {
    public Set<Id> accounts {get; set;}
     public  Map<id,id> contractgroupid {get; set;}

    public UpdateRelatedInternalContractIds(Set<Id> accounts,map<id,id> contractgroupid) {
        this.accounts = accounts;
        this.contractgroupid= contractgroupid;
    }

    public void go() {
        Set<Id> contractGroups = new Set<Id>();

        Map<String, Set<String>> customers = new Map<String, Set<String>>();
  
        if (accounts == null)
            return;
      
       List<Customer_Groups_Association__c> clist = [select id, Internal_Contract_Group__c, Internal_Contact_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts];

        if (clist != null) {
            for (Customer_Groups_Association__c c : clist) {
                contractGroups.add(c.Internal_Contract_Group__c);

                Set<String> t = customers.get(c.Internal_Contact_Group__c);

                if (t == null) {
                    t = new Set<String>();
                    customers.put(c.Internal_Contact_Group__c, t);
                }

                t.add(c.Internal_Contract_Group__c);
            }
        }
        
       

        Map<String, Set<String>> contractIds = new Map<String, Set<String>>();

        List<Contract_Group_Association__c> dlist = [select id, Internal_Contract__r.Internal_Contract_ID__c, Internal_Contract_Group__c from Contract_Group_Association__c where Internal_Contract_Group__c in :contractGroups];

        if (dlist != null) {
            for (Contract_Group_Association__c d : dlist) {
                Set<String> t = contractIds.get(d.Internal_Contract_Group__c);

                if (t == null) {
                    t = new Set<String>();
                    contractIds.put(d.Internal_Contract_Group__c, t);
                }

                t.add(d.Internal_Contract__r.Internal_Contract_ID__c);
            }
        }

        List<Account> customerList = [select id, Related_Internal_Contract_IDs__c from Account where id in :accounts];

        for (Account c : customerList) {
            String result = '';

            if (customers.containsKey(c.id)) {
                system.debug('customer@@' + c);
                for (String s : customers.get(c.id)) {
                    if (!contractIds.containsKey(s))
                        continue;

                    for (String x : contractIds.get(s)) {
                        if (result != '')
                            result += ',';

                        result += x;    
                    }
                }
            }

            c.Related_Internal_Contract_IDs__c = result;
        }

        update customerList;
    }
    
NagendraNagendra (Salesforce Developers) 
Hi Vinothini,

To start with, remove following if conditions from your trigger
if (Trigger.old != null) { and if (Trigger.new != null) {
because they are unnecessary, as you are looping through both of the collections, besides, triggerwon't fire if there is no record. So, logically that was a waste.

Now, put a check before you SOQL here like
if (cons.size> 0) List<Customer_Groups_Association__c> conGroup = [select Internal_Contact_Group__c from Customer_Groups_Association__c where Internal_Contract_Group__c in :cons];
Problem in your handler class
List<Customer_Groups_Association__c> clist = [select id, Internal_Contract_Group__c, Internal_Contact_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts];
You are querying again Customer_Groups_Association__c (on line 16)with the same filters as used in your trigger (also mentioned above). This is another waste, and with the increase in data you might even hit too many SOQL (101) error in single execution context.

Make it a part of your constructor, as you did with accounts.

Then, remove the if check, which is unnecessary for the same reason as explained above
if (clist != null) { // remove this as well
and
if (dlist != null) {
Also, as mentioned in another answer, wrap your code to logical blocks (what you wanna do when the record is updated/deleted/inserted) using trigger context variables https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm.

Hope this helps.

Kindly mark this as solved if it's resolved.

Thanks,
Nagendra
Vinothini murugesh 10Vinothini murugesh 10
Hi Nagendra,

thank you so much for your input. This problem has solved.