function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Execution of Batch size 200 In trigger @salesforce developement

Hi ,

     When I am Deleting Account records as Bulk Data Deletion.. My trigger will fire. that time my first Query will execute. and if the size of Query list Have 200. this time  i want to Execute second Query as 200 times . Because of Governar limits it will showing Exception as:Too many SOQL queries: 101” .But i want to execute Second query 200 times  because of Batch size 200 as default . so my requirement is second query has to execute 200 times.   

I am posting my code here pls look once  and give suggestions.



trigger OnAccountDeleteHandleSecondaryAccountsTrigger on Account (before delete) {

List<Account> accountList = [SELECT Id, Primary_Account__c, Zyme_Id__c, Zyme_AKA__c, Partner_Reporting__c, Partner_Level__c FROM Account WHERE Primary_Account__c = NULL AND Id IN :Trigger.oldMap.keySet()];
System.debug(Logginglevel.INFO,'accountList size : '+accountList.size());
List<Account> secondaryAccountsForUpdate = new List<Account>();
List<Account> accountsForUpdate = new List<Account>();
if(trigger.isBefore) {
if(trigger.isDelete) {
if(accountList.size() > 0) {
for(Account acct: accountList) {
//System.debug(Logginglevel.INFO,'in for : '+accountList.size());
try {
Account[] secondaryAccounts = [SELECT Id, Primary_Account__c, Zyme_Id__c, Zyme_AKA__c, Partner_Reporting__c, Partner_Level__c FROM Account WHERE Zyme_Id__c = NULL AND Primary_Account__c = :acct.Id AND Zyme_AKA__c = :acct.Zyme_Id__c ORDER BY CreatedDate ASC LIMIT 1];
if(secondaryAccounts != null && secondaryAccounts.size() > 0) {
Account secondaryAccount = secondaryAccounts[0];
System.debug(Logginglevel.INFO,'got sec account : '+secondaryAccount.Id);
if (secondaryAccount != null) {
secondaryAccount.Primary_Account__c = null;
secondaryAccount.Zyme_Id__c = acct.Zyme_Id__c;
secondaryAccount.Zyme_AKA__c = acct.Zyme_AKA__c;
secondaryAccount.Partner_Reporting__c = acct.Partner_Reporting__c;
secondaryAccount.Partner_Level__c = acct.Partner_Level__c;

acct.Zyme_Id__c = null;
acct.Zyme_AKA__c = null;

} catch(Exception e) {
System.debug(Logginglevel.ERROR,'got exception in fetching sec account : ');
System.debug(Logginglevel.INFO,'secondaryAccountsForUpdate size : '+secondaryAccountsForUpdate.size());
if(secondaryAccountsForUpdate.size() > 0) {
update accountsForUpdate;
update secondaryAccountsForUpdate;




          is There any way to do  this .  please suggest some some solutions.



Best Answer chosen by Admin (Salesforce Developers) 



You need to bulkify the trigger, the best practice is that don't put any SOQL query or DML satement within loop, it can hit your GOV limit as in your case.


So here the solution is that just take out that SOQL query from the loop, write this query before loop then store query result in map and use that map in loop to get data.