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
Max_gMax_g 

Trigger generating Too many script statements: 200001 error

The following trigger is giving me this error when I have more than 20 account records to update.  This is supposed to take a custom account field and get the corresponding parent salesforce id. 

Any code help will be appreciated. 

 

Here is the code:

trigger

GetParentBulked onAccount (beforeinsert, beforeupdate) {

list

<Account> listtoupdate = newlist<Account>();       

 

       

// For loop to iterate through all the queried Sales History records

         

 

list<string> JDE = newlist<string>();      

for(Account  h1: Trigger.new){

if (Trigger.isUpdate){   

if(h1.Parent_Account_JDE__c != Trigger.oldMap.get(h1.Id).Parent_Account_JDE__c){

JDE.add(h1.Parent_Account_JDE__c);

system.debug('H1 Cust = ' + h1.JDE_CUSTOMER__c);  

}

}

if(Trigger.isinsert){   

JDE.add(h1.Parent_Account_JDE__c);

system.debug('H1 Cust = ' + h1.JDE_CUSTOMER__c);  

}

}

 

Map<id,Account> a = newmap<id, account>([Select Id,Name,JDE_Customer__c,Parent_Account_JDE__c fromAccountwhere JDE_Customer__c in: JDE]) ;

 

for(Account h: Trigger.new){  

for(id A1 : a.keyset() ){  

    

account A2 = a.get(A1); 

system.debug('a =' + a);

if(a.size() > 0){  

 

 

system.debug('A2 = ' + a2);

 system.debug('A2 Parent = ' + a2.Parent_Account_JDE__c);

 system.debug('H Jde Cust =' + h.JDE_CUSTOMER__c);

 system.debug('H Parent = ' + h.Parent_Account_JDE__c);

 system.debug('H Parent ID =' + h.ParentId); 

 system.debug('A2 Id =' + a2.Id);

 system.debug('A2 JDE Customer = ' + a2.JDE_CUSTOMER__c);  

  

try{   

if(a2.JDE_CUSTOMER__c != null)                       

h.ParentId = A2.Id;

if(a2.JDE_CUSTOMER__c == null){

 if(h.ParentId != null)                       

h.ParentId =null;

}

system.debug('H Parent after Update = ' + h.ParentId);

system.debug('H = ' + h);             

  

}         

catch(Exception e)     

{       

system.debug('No Account Record ---- ' + e);     

}                           

                 

listtoupdate.add(h); 

}  

}                 

}      

}

crop1645crop1645

Two first-brush thoughts:

 

1 - Your select statement where you compare where id IN :JDE

 

if JDE contains any null values, you'll find lots of Accounts I suspect and subsequent for loops may be lengthy

 

2 - You reference listToUpdate but nothing ever happens to this -- are you omitting code that does DML on this? If so, you'll get recursive triggers and run out of script statements

Max_gMax_g

Trigger seems to be working correctly for small numbers of records. 

 

My understanding is the listtoupdate will automatically update the accounts when the trigger finishes.

 

If I am incorrect, please enlighten me.

 

 

crop1645crop1645

listToUpdate should be removed; SFDC will implicitly update/insert the values in Trigger.new

 

as for the too many script statements, this can be resolved by using system.debug.  Try making them all at System.debug(LoggingLevel.INFO,'my message') and run the test at logginglevel INFO -- this will filter out all the sfdc noise in the logs and make it easier to see what is taking so many statements.

 

You can also use the Limits.xxx methods to see how many script statements you have consumed per each 'interesting point' in your code - check the Apex doc