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
trublutrublu 

Trigger - Too many script statements: 50001

Hi all,
 
In order to batch up the soql queries within my Account trigger (trigger on account after update), I have some loops to build the id lists (account, user, group, parent account) up front in the trigger. However, instead of getting "too many SOQL script" error, I am now getting "too many statement script" when the trigger is invoked via batch account update.
 
Could anyone please help??? SF is so restrictive that I cannot really do anything with triggers!!!
 
Thanks a lot.
 
PS I have to build out all these id lists because I need to insert AccountShare records based on a specified user in a custom field on Account... Frankly, the Account Trigger does not really update the account record itself, but to add the appropriate AccountShare records based on the user role of that specified user on the custom Account field.
 
======================================================================

Force.com Sandbox

 

Apex script unhandled trigger exception by user/organization:

 

updateAccountAfterUpdate: execution of AfterUpdate

 

caused by: System.Exception: Too many script statements: 50001

 

Class.ProspectManagerManager.handleAccountSharing: line 62, column 21

Trigger.updateAccountAfterUpdate: line 13, column 9

SuperfellSuperfell
check your loop logic, this means you executed 50,000 statements without touching the DB, so the server thinks you're in an infinite loop.
trublutrublu
Here are some of the loops in the triggers.... i dont seem to have any better way to write these loops...
 

//1. childAccount

for (Integer i = 0; i < accountList.size(); i++) {

childAccountIdMap.put(i, accountList[i].id);

childAccountIdList.add(accountList[i].id);

prospectManagerUserIdMap.put(i, accountList[i].Prospect_Manager__c);

prospectManagerUserIdList.add(accountList[i].Prospect_Manager__c);

}

try{

childAccountList = [SELECT id, Prospect_Manager__c from Account WHERE Id in :childAccountIdList];

}

catch(Exception e)

{

System.debug('childAccountList is null');

}

if (childAccountList != null) {

for (Integer i = 0; i < accountList.size(); i++) {

for (Integer ii = 0; ii < childAccountList.size(); ii++) {

Account thisAccount = childAccountList.get(ii);

if ((''+thisAccount.id).equals(accountList[i].id)){

childAccountMap.put(i, thisAccount);

continue;

}

}

}

}

//2. prospectManager

try{

prospectManagerUserList = [select userroleid, id, name, Development_Unit__c from User where id in :prospectManagerUserIdList];

}

catch(Exception e)

{

System.debug('prospectManagerUserList is null');

}

if (prospectManagerUserList != null && !prospectManagerUserList.isempty()) {

for (Integer i = 0; i < accountList.size(); i++) {

for (Integer ii = 0; ii < prospectManagerUserList.size(); ii++) {

User thisMgr = prospectManagerUserList.get(ii);

if (thisMgr != null && (''+thisMgr.id).equals(accountList[i].prospect_manager__c)){

prospectManagerUserMap.put(i, thisMgr);

continue;

}

}

}

}

//3. GROUP

AND ONE MORE SECTION FOR GROUP....

SuperfellSuperfell
rather than using nested loops, create a map from your query results, so that you can do lookups rather than the extra loop.
jvolkovjvolkov

Simon,

I am having the same issue, , here is my trigger...will the same solution apply in this case?

Code:
public class testCopyLivedateToAccount
{
    static TestMethod void testCopyLiveDate()
    {
        Account a = new Account();
        a.Name='newAccount';
        insert a;
    
        Contact con1 = new Contact();
        con1.Email ='sdpgoel@yahoo.com';
        con1.LastName='Divya';
        insert con1;
    
        BDC_Account__c CRMSnapShot = new BDC_Account__c();
        CRMSnapShot.Email__c='sdpgoel@yahoo.com';
        CRMSnapShot.Live_Date__c = Date.newInstance(2006,10,10);
        CRMSnapShot.Account_UUID__c='1888768845654654';
        CRMSnapShot.Account__c=a.Id;
        insert CRMSnapShot;
        
        con1.Email ='sdpgoel1@yahoo.com';
        update con1;
        
        CRMSnapShot.Email__c='sdpgoel1@yahoo.com';
        update CRMSnapShot;

        // Code Inserted by SA On 1:16 AM IST 12/19/2008
        BDC_Account__c CRMSnapShot1 = new BDC_Account__c();
        CRMSnapShot1.Email__c='sdpgoel@yahoo.com';
        CRMSnapShot1.Live_Date__c = Date.newInstance(2005,10,10);
        CRMSnapShot1.Account_UUID__c='1888769845654654';
        CRMSnapShot1.Account__c=a.Id;
        insert CRMSnapShot1;

        // Code Inserted by SA On 1:12 AM 12/20/2008           
        BDC_Account__c CRMSnapShot2 = new BDC_Account__c();
        CRMSnapShot2.Email__c='sdpgoel@yahoo.com';
        CRMSnapShot2.Live_Date__c = Date.newInstance(2007,10,10);
        CRMSnapShot2.Account_UUID__c='1888769845634654';
        CRMSnapShot2.Account__c=a.Id;
        insert CRMSnapShot2;

  // Code Inserted by SA On 4:45 PM 12/30/2008 
        Account a1 = new Account();
        a1.Name='newAccount';
        insert a1;  

  // Code Inserted by SA On 4:45 PM 12/30/2008              
        CRMSnapShot2.Account__c=a1.Id;
  update CRMSnapShot2;

        // Code Inserted by SA On 1:16 AM IST 12/19/2008        
        delete CRMSnapShot;     
    }
}

 

-Thanks