+ Start a Discussion
SF Beginner 2019SF Beginner 2019 

apex cpu limit on load of 50 records

I have this code wherein it seems that even when run into small bulk like 50 updating it causing apex cpu limit is there a way I can remove that cpu limit
 
public class depToAccount {
    public static void deptpAccountV1(List<Dependent__c> depmem) {
        Set<Id> accountIdSet = new Set<Id>();
        Set<Id> coverIdSet = new Set<Id>();
        Map<Id, List<Dependent__c>> atoDep = new Map<Id, List<Dependent__c>>();
        Map<Id, List<Dependent__c>> ctomeddep = new Map<Id, List<Dependent__c>>();
        Map<Id, List<Dependent__c>> cto19belowdep = new Map<Id, List<Dependent__c>>();
        Map<Id, List<Dependent__c>> ato19belowdep = new Map<Id, List<Dependent__c>>();
        Map<Id, List<Dependent__c>> cto20belowdep = new Map<Id, List<Dependent__c>>();
        Map<Id, List<Dependent__c>> ato20updep = new Map<Id, List<Dependent__c>>();//
        List<CampaignMember> cmList = new List<CampaignMember>();
        
        Map<Id, Account> accountToUpdateMap = new Map<Id, Account>();
        
        
        for(Dependent__c depm : depmem){
            if(depm.status__c == 'ACTIVE' && depm.Catg__c == 'Vision'){
                if(depm.Account__c != null){
                    accountIdSet.add(depm.Account__c);
                }
                if(depm.Cover__c != null){
                    coverIdSet.add(depm.Cover__c); 
                }
            }
        }
        
        List<Dependent__c> existingDependent = [SELECT Id, Account__c, Account__r.Name, Cover__c, Cover__r.Comply__c,Rel__c, status__c, Catg__c, Age_Today__c 
                                                            FROM Dependent__c 
                                                            WHERE ((Cover__c IN: coverIdSet AND Cover__c != null)
                                                                   OR (Account__c IN: accountIdSet AND Account__c != null))
                                                            AND status__c ='ACTIVE' AND Plan_Name__c != NULL aND Catg__c != NULL];
        for(Dependent__c depM : existingDependent){
            if(depM.Age_Today__c < 19){
                if(cto19belowdep.containsKey(depM.Cover__c)){
                    cto19belowdep.get(depM.Cover__c).add(depM);
                }
                else {
                    cto19belowdep.put(depM.Cover__c, new List<Dependent__c>{ depM });
                }
                
                if(ato19belowdep.containsKey(depM.Account__c)){
                    ato19belowdep.get(depM.Account__c).add(depM);
                }
                else {
                    ato19belowdep.put(depM.Account__c, new List<Dependent__c>{ depM });
                }
            }
            if(depM.Age_Today__c >= 19){
                if(cto20belowdep.containsKey(depM.Cover__c)){
                    cto20belowdep.get(depM.Cover__c).add(depM);
                }
                else {
                    cto20belowdep.put(depM.Cover__c, new List<Dependent__c>{ depM });
                }
                
                if(ato20updep.containsKey(depM.Account__c)){
                    ato20updep.get(depM.Account__c).add(depM);
                }
                else {
                    ato20updep.put(depM.Account__c, new List<Dependent__c>{ depM });
                }
            }
            if(depM.Catg__c == 'Vision'){
                if(ctomeddep.containsKey(depM.Cover__c)){
                    ctomeddep.get(depM.Cover__c).add(depM);
                }
                else {
                    ctomeddep.put(depM.Cover__c, new List<Dependent__c>{ depM });
                }
            }
            
            if(depM.Catg__c == 'Dental'){
                if(atoDep.containsKey(depM.Account__c)){
                    atoDep.get(depM.Account__c).add(depM);
                }
                else {
                    atoDep.put(depM.Account__c, new List<Dependent__c>{ depM });
                }
            }
        }
        Map<Id, Account> acctAttesAppMap = new Map<Id, Account>([Select ID,Good__c,Age_in_Numbers__pc,PersonContactId,Phone__c ,NumberofYears__c  FROM Account where ID IN : accountIdSet ]);
        
        for(Id acctId : accountIdSet){
            for(Dependent__c depM : existingDependent){
                for(Id covId : coverIdSet){
                    if(cto19belowdep.containsKey(covId) && depM.Cover__c==covId){ 
                        for(Dependent__c depU19 : cto19belowdep.get(covId)){
                            if(depU19.Age_Today__c < 19){ 
 
                                if((atoDep.containsKey(acctId)) || (acctAttesAppMap.get(acctId).Good__c == true) || (acctAttesAppMap.get(acctId ).NumberofYears__c > 0 )){ 
                                    if(ctomeddep.containsKey(covId)){
                                        for(Dependent__c depMemMedi : ctomeddep.get(covId)){ 
                                            accountToUpdateMap.put(depMemMedi.Account__c, new Account(Id=depMemMedi.Account__c, Compliant__c=true, Comply__c=false));
                                        }
                                    }
                                    
                                }
                                else {
                                    if((!atoDep.containsKey(acctId)) || (acctAttesAppMap.get(acctId).Good__c == false) || (acctAttesAppMap.get(acctId ).NumberofYears__c == 0 )){ 
                                        if(ctomeddep.containsKey(covId)){
                                            for(Dependent__c depMemMedi : ctomeddep.get(covId)){
                                                accountToUpdateMap.put(depMemMedi.Account__c, new Account(Id=depMemMedi.Account__c, Compliant__c=false, Comply__c=false));
                                            }
                                        }
                                        
                                    }
                                    IF(acctAttesAppMap.get(acctId).Phone__c != NULL ){
                                        {
                                           
                                            cmList.add( new CampaignMember(
                                                CampaignId  = System.Label.Doctor_Campaign,
                                                ContactId = acctAttesAppMap.get(acctId).PersonContactId,
                                                Status = 'Completed'
                                            ));

                                            
                                        }
                                    }
                                    IF(acctAttesAppMap.get(acctId).Phone__c == NULL && acctAttesAppMap.get(acctId).Age_in_Numbers__pc >= 19){
                                        {
                                            cmList.add( new CampaignMember(
                                                CampaignId  = System.Label.Nurse_Campaign,
                                                ContactId = acctAttesAppMap.get(acctId).PersonContactId,
                                                Status = 'Completed'
                                            ));
                                        }
                                    }
                                    IF(acctAttesAppMap.get(acctId).Phone__c == NULL &&  acctAttesAppMap.get(acctId).Age_in_Numbers__pc < 19){
                                        {
                                            cmList.add( new CampaignMember(
                                                CampaignId  = System.Label.Staff_Campaign,
                                                ContactId = acctAttesAppMap.get(acctId).PersonContactId,
                                                Status = 'Completed'
                                            ));
                                        }
                                    }
                                }
                            }
                            
                        }
                    else if(cto20belowdep.containsKey(covId) && depM.Cover__c==covId){
                        for(Dependent__c depU20 : cto20belowdep.get(covId)){
                            if(depU20.Age_Today__c >= 19){ 
                                if(atoDep.containsKey(acctId) ){
                                    if(ctomeddep.containsKey(covId)){
                                        for(Dependent__c depMemMedi : ctomeddep.get(covId)){
                                            accountToUpdateMap.put(depMemMedi.Account__c, new Account(Id=depMemMedi.Account__c, Compliant__c=false, Comply__c=true));
                                        }
                                    }
                                }
                            }
                            
                        }
                    }
                    if(depM.Cover__r.Comply__c==true && depM.Cover__c == covId){
                        if(ctomeddep.containsKey(covId)) 
                            for(Dependent__c CMmed : ctomeddep.get(covId)){
                                accountToUpdateMap.put(CMmed.Account__c, new Account(Id=CMmed.Account__c, Compliant__c=false, Comply__c=true));
                            }
                    } 
                    
                }
            }
        }
        database.insert(cmlist, false);
        update accountToUpdateMap.values();
        
    }
    
}

 
Ajay K DubediAjay K Dubedi
Hi,

Try implementing the following:
1. Try including limit 100 , 1000... (as per your requirement) in your queries at lines 27 and 81. Do this as per data required only try not to bring extra data.
2. Use maps (if possible) for query at line 27.
3. Your code block (83-140) is more likely to create problems so try commenting out some parts of it (like inner for loop) to find out which code block is exactly creating problems. If found try reducing that loop by using maps in place of two for loops.

For more on CPU limits here's the official salesforce documentation:  https://help.salesforce.com/articleView?id=000339361&language=en_US&type=1&mode=1

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi