You need to sign in to do that
Don't have an account?

Too Many Soql in Account Trigger
Hi
I am getting this issue to many soql, is there something wrong with my code when try to save a record this error appers that too many soql
I am getting this issue to many soql, is there something wrong with my code when try to save a record this error appers that too many soql
public class AccountTask { public static void accountTaskCopy(List<Account> acclist){ Id EmailRT = SObjectType.Task.getRecordTypeInfosByDeveloperName().get('Email').getRecordTypeId(); Id CallRT = SObjectType.Task.getRecordTypeInfosByDeveloperName().get('Call').getRecordTypeId(); Set<Id> forWhatID = new Set<Id>(); Set<Id> accID = new Set<Id>(); Set<Id> coverageIds = new Set<Id>(); Map<ID, List<CM__c>> accountIdToListCMOMap = new Map<Id, List<CM__c>>(); Map<ID, List<CM__c>> coverageIdToListCVMMap = new Map<Id, List<CM__c>>(); Map<ID, List<Task>> accountIdToListTaskCompletedMap = new Map<Id, List<Task>>(); Map<ID, List<Task>> accountIdToListTaskNotCompletedMap = new Map<Id, List<Task>>(); Map<Id, Account> accountsToUpdate = new Map<Id, Account>(); for(Account acc: acclist){ { accID.add(acc.ID); } } for(Task tsk : [SELECT ID,whatid FROM TASK where WHATID IN : accID]){ if(tsk.WhatID != Null && tsk.WhatID.getSObjectType() == Account.SobjectType){ forWhatID.add(tsk.WhatId); System.debug('forWhatID' + forWhatID); } } List<CM__c> existingCMs = [SELECT Id, Account__c, Account__r.Name, CVR__c, Coverage__r.Good__c,rel__c, status__c, BusinessType__c, Age_Today__c FROM CM__c WHERE (CVR__c != null AND Account__c IN: forWhatID AND Account__c != null) AND status__c ='ACTIVE' AND BusinessType__c = 'Medical']; for(CM__c cMo : existingCMs){ if(accountIdToListCMOMap.containsKey(cMo.Account__c)){ accountIdToListCMOMap.get(cMo.Account__c).add(cMo); } else{ accountIdToListCMOMap.put(cMo.Account__c, new List<CM__c>{cMo}); } if(coverageIdToListCVMMap.containsKey(cMo.CVR__c)){ coverageIdToListCVMMap.get(cMo.CVR__c).add(cMo); } else{ coverageIdToListCVMMap.put(cMo.CVR__c, new List<CM__c>{cMo}); } coverageIds.add(cMo.CVR__c); } List<CM__c> existingCMFromCov = [SELECT Id, Account__c, Account__r.Name, CVR__c, Coverage__r.Good__c,rel__c, status__c, BusinessType__c, Age_Today__c FROM CM__c WHERE (CVR__c != null AND CVR__c IN: coverageIds AND Account__c != null) AND status__c ='ACTIVE' AND BusinessType__c = 'Medical']; for(CM__c cMo : existingCMFromCov){ if(coverageIdToListCVMMap.containsKey(cMo.CVR__c)){ coverageIdToListCVMMap.get(cMo.CVR__c).add(cMo); } else{ coverageIdToListCVMMap.put(cMo.CVR__c, new List<CM__c>{cMo}); } } System.debug('accountIdToListCMOMap ' + accountIdToListCMOMap); System.debug('coverageIdToListCVMMap ' + coverageIdToListCVMMap); List<Task> taskCompletedOrInvalidPhone = [SELECT Id, whatid, CallCustom__c, RecordtypeId FROM Task WHERE Whatid IN :forWhatID AND (CallCustom__c = 'Completed Campaign Requirements' OR CallCustom__c = 'Invalid Phone') AND WhatID != null]; for(Task tsk : taskCompletedOrInvalidPhone){ if(accountIdToListTaskCompletedMap.containsKey(tsk.WhatId)){ accountIdToListTaskCompletedMap.get(tsk.WhatId).add(tsk); } else{ accountIdToListTaskCompletedMap.put(tsk.WhatId, new List<Task>{tsk}); } } List<Task> taskNotCompleteCampaign = [SELECT Id, whatid,CallCustom__c, RecordtypeId FROM Task WHERE Whatid IN :forWhatID AND RecordTypeID =: CallRT AND (CallCustom__c = 'Left Message' OR CallCustom__c = 'Unable to Reach') AND WhatID != null ]; for(Task tsk : taskNotCompleteCampaign){ if(accountIdToListTaskNotCompletedMap.containsKey(tsk.WhatId)){ accountIdToListTaskNotCompletedMap.get(tsk.WhatId).add(tsk); } else{ accountIdToListTaskNotCompletedMap.put(tsk.WhatId, new List<Task>{tsk}); } } System.debug('accountIdToListTaskCompletedMap ' + accountIdToListTaskCompletedMap); System.debug('accountIdToListTaskNotCompletedMap ' + accountIdToListTaskNotCompletedMap); for(Id acctId : forWhatID){ System.debug('acctId ' + acctId); if(accountIdToListTaskCompletedMap.containsKey(acctId)){ system.debug('acctIdlist' + accountIdToListTaskCompletedMap); if(accountIdToListCMOMap.containsKey(acctId)){ for(CM__c cMo : accountIdToListCMOMap.get(acctId)){ System.debug('cMo ' + cMo); if(coverageIdToListCVMMap.containsKey(cMo.CVR__c)){ for(CM__c covMem2 : coverageIdToListCVMMap.get(cMo.CVR__c)){ System.debug('covMem2 ' + covMem2); accountsToUpdate.put(covMem2.Account__c, new Account(Id=covMem2.Account__c, PDC__c=true)); } } } } } } System.debug('accountsToUpdate ' + accountsToUpdate); update accountsToUpdate.values(); } }
Regards
Karthik
All Answers
Error: System.LimitException: Too many SOQL queries: 101 comes when you cross the Governors Limit
please refer to the below link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
And make sure your trigger cannot execute repeatedly. You have to use best practice always.
Best practice link:
https://developer.salesforce.com/index.php?title=Apex_Code_Best_Practices&oldid=26951
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
It might be a case of Recursive trigger. Have you checked whether the trigger is fired more than once ?. Is the class AccountTask is called from after update Trigger. Most of the time in after update trigger the update will cause the trigger to fire again resulting in recursive trigger. Please update the trigger code also to have a clear picture about the issue.
If the trigger is fired more than once you have to restrict with some flag to make it run only once.
Regards
Karthik
trigger AccountAfterUpdate on Account (After Update) {
AccountTask.accountTaskCopy(trigger.new);
}
Regards
Karthik
Thank you! so meaning the cause for these one is that the account was updated, then updated again?