+ Start a Discussion
Vinothini Murugesh 23Vinothini Murugesh 23 

apex cpu time limt exceeded error for my code.any one please help me correct it.

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();
   
}

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>();
          Set<String> t=new Set<String>();
        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);

              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) {
              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;
    }
    
   
     public void goNew() {
          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 (select  Internal_Contract_Group__c from Customer_Groups_Association__c where Internal_Contact_Group__c in :accounts)];
          Map<Id,String> relatedIds = new Map<Id,String>();
         if(!dlist.isEmpty()){
         for(Contract_Group_Association__c conAss : dlist){               
            //conAss.Internal_Contract__r.Internal_Contract_ID__c;
         }
         }  
         
         
     }
}
Dushyant SonwarDushyant Sonwar
Hi Vinothini ,

Your have for loop inside for loop inside for loop.
Example : if you have 10 records so your loop will run (10*10*10) times 
for{integer i= 0 ; i<10 ;i++){
   for{integer j= 0 ; j<10 ;j++){
       for{integer j= 0 ; j<10 ;i++){
       }
    }

}

Your below code is causing the apex cpu time limit exceeded issue.
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;    
                    }
                }
            }
Try to refactor/optimize your code to into one or two inner loops.

 
Vinothini Murugesh 23Vinothini Murugesh 23
Hi Dushyant, thanks for ur input. will check this