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
AashikaAashika 

System.Exception- Too many script statements: 50001

Hi there,

The following errors are being caused through the API call from the client's proprietary system to Salesforce. It is caused by the following Trigger. However, when I run tests for the Test Class for this Trigger it displays 93% test coverage. The Trigger is already placed in production and it is causing this error.

The Error is the following:-
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Trigger.CopyLiveDateToAccount.CopyLiveDate: line 103, column 17

Trigger.CopyLiveDateToAccount: line 49, column 5: [Ljava.lang.String;@3a38f3

2009-01-04@06:19:33,260|NDC: 1231077615800467873150|ERROR|business.workflow.BdcAccountInSalesForceDAO.|[JMS SessionPool Worker-265]|BdcAccountInSalesForceDAO.upsert: failed to upsert object with bdcAccountId: 98D50404-8144-4C03-BB82-047B55A3F6B6, User Id: tmarkel@credentrust.com and Salesforce ID: null

2009-01-04@06:19:33,260|NDC: 1231077615800467873150|ERROR|business.workflow.BdcAccountInSalesForceDAO.|[JMS SessionPool Worker-265]|BdcAccountInSalesForceDAO.upsert: API error(s):

2009-01-04@06:19:33,260|NDC: 1231077615800467873150|ERROR|business.workflow.BdcAccountInSalesForceDAO.|[JMS SessionPool Worker-265]|[CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY] CopyLiveDateToAccount: execution of AfterUpdate

 

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

///////////////////////////////////////////////////////////////////////////////////////////////////



Here is the Trigger:-
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Whenever a CRM Snapshot record will be inserted trigger will look all the CRM Snapshot 
//records for least "Live Date" of CRM SnapShot and copy to "Live Date" field of Account.
trigger CopyLiveDateToAccount on BDC_Account__c (after insert, after update,after delete)
{
BDC_Account__c[] bdcAccounts = new BDC_Account__c[]{};
// Code Inserted by SA On 4:45 PM 12/30/2008
BDC_Account__c[] bdcAccountsOld = new BDC_Account__c[]{};
//If the record is inserted
// Code Inserted by SA On 4:45 PM 12/30/2008
if(Trigger.isInsert)
{
bdcAccounts = Trigger.new;
}
// Code Inserted by SA On 4:45 PM 12/30/2008
else if(Trigger.isUpdate)
{
bdcAccounts = Trigger.new;
bdcAccountsOld = Trigger.old;
}
// Code Inserted by SA On 1:16 AM IST 12/19/2008
else if(Trigger.isDelete)
{
bdcAccounts = Trigger.old;
}

Set <ID>setAccountIds = new Set <ID>();
for(BDC_Account__c crmSnapShot: bdcAccounts)
{
if(crmSnapShot.Account__c != null)
{
setAccountIds.add(crmSnapShot.Account__c);
}
}

// Code Inserted by SA On 4:45 PM 12/30/2008
Set <ID>setAccountIdsToBeNull = new Set <ID>();
for(Integer i=0;i<bdcAccountsOld.size();i++)
{
if(bdcAccountsOld[i].Account__c != null && bdcAccounts[i].Account__c == null)
{
setAccountIdsToBeNull.add(bdcAccountsOld[i].Account__c);
}
else if(bdcAccountsOld[i].Account__c != null && (bdcAccountsOld[i].Account__c != bdcAccounts[i].Account__c))
{
setAccountIdsToBeNull.add(bdcAccountsOld[i].Account__c);
}
}

CopyLiveDate(bdcAccounts,setAccountIds);

// Code Inserted by SA On 4:45 PM 12/30/2008
if(Trigger.isUpdate && setAccountIdsToBeNull.size()>0)
CopyLiveDate(bdcAccountsOld,setAccountIdsToBeNull);


//this function will copy the least Live date from CRM Snapshot object to Account
// Code Inserted by SA On 4:45 PM 12/30/2008
private void CopyLiveDate (BDC_Account__c[] crmSnapShots,Set<ID> setAccountIdsSet)
{

System.debug('SA' + setAccountIds);
BDC_Account__c [] bdcAccs = [select id, Account__c, Live_Date__c from BDC_Account__c where Account__c in:setAccountIdsSet];
Account [] AccountsToBeUpdated = new Account[]{};
for (BDC_Account__c acc: crmSnapShots)
{
Date lessDate = null;
// Code Inserted by SA On 1:12 AM 12/20/2008
Date lastLiveDate = null;
// Code Inserted by SA On 1:16 AM IST 12/19/2008
System.debug('SA CurrentChilds' + crmSnapShots);
System.debug('SA AllChilds' + bdcAccs);
//Get the least Live date
for(BDC_Account__c allBdcAcc: bdcAccs)
{
if(allBdcAcc.Account__c == acc.Account__c)
{
if (lessDate == null)
{
lessDate = allBdcAcc.Live_Date__c;
}
else
{
if (allBdcAcc.Live_Date__c<lessDate)
{
lessDate = allBdcAcc.Live_Date__c;
}
}
System.debug('SA Final Date' + lessDate);

// Code Inserted by SA On 1:12 AM 12/20/2008
if (lastLiveDate == null)
{
lastLiveDate = allBdcAcc.Live_Date__c;
}
else // Code Inserted by SA On 1:12 AM 12/20/2008
{
if (allBdcAcc.Live_Date__c>lastLiveDate)
{
lastLiveDate = allBdcAcc.Live_Date__c;
}
}
}
System.debug('SA Last Live Date' + lastLiveDate);
}
if(acc.Account__c != null)
{
Account a = new Account(Id=acc.Account__c);
a.Live_Date__c= lessDate;
// Code Inserted by SA On 1:12 AM 12/20/2008
a.Last_Live_Date__c= lastLiveDate;
System.debug('SA Account To be Updated' + a);
AccountsToBeUpdated.add(a);
}
}
if(AccountsToBeUpdated.size()>0)
{
update AccountsToBeUpdated;
}// Update Account
}
///////////////////////////////////////////////////////////


Could someone please advice what is causing these errors? Is it the If-Else statements, too many loops.???
}

aalbertaalbert
The "too many script statements" exception is thrown because over the maximum 50,000 apex lines are executed during the trigger invocation.
Trigger context is 10,000 statements plus 200 times the number of records in the top level call. For example, a DML statement that processes 200 records is subject to a limit of 10,000 + 200*200, which equals 50,000 statements.

More information here about governor limits: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_gov_limits.htm

So the goal is to refactor the apex code to not have any unnecessary for loops, or to combine multiple for loops into a single loop (for example).