You need to sign in to do that
Don't have an account?
SF Beginner 2019
apex cpu limit loading around 1.2M record
HI
I am encountering and error while doing a data load.
here is my code when dataloading. is there anything that needs to be optimize in here so far?
I am encountering and error while doing a data load.
here is my code when dataloading. is there anything that needs to be optimize in here so far?
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(); } }
I would like to make a couple of suggestion:
1. You haven't used limit parameter inside any of your SOQL queries this can lead to exception. So try using LIMIT 100, 1000 or max 10000 as per your requirement.
2. Futher in line number 85 you have used 3rd nested for loop, 4th and so on, it is not considered as a good practice, so try replacing it by a combination of maps.
Add above two 'Good practices' and your code is good to go.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
. You haven't used limit parameter inside any of your SOQL queries this can lead to exception. So try using LIMIT 100, 1000 or max 10000 as per your requirement.
will does run all records if I limit them to 10000?
Thank you
Yes, it would. Limit just sets maximum number of hits your query can make.
Thanks,
Ajay Dubedi
how can I convert this into map?