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
CloudConversionCloudConversion 

Suggestions on "Too many script statements" error in Trigger?

We're also getting the "Too many script statements" error in another one of our triggers.

 

Any ideas?

 

Thanks,
Brad

 

Here is our code:

 

 

trigger OrderLineTrigger on Order_Line_Item__c (after insert, after update) { List<Case> casesToUpdate = new List<Case>(); Set<String> itemIds = new Set<String>(); Set<Id> contactIds = new Set<Id>(); for(Order_Line_Item__c o : trigger.new) { itemIds.add(o.Marketplace_Item_ID__c); contactIds.add(o.Contact__c); } List<Case> caseList = new List<Case>([Select Id, Item_Id__c, Related_To_Order__c, Related_To_Order_Line__c, SKU__c, ContactId, Contact.Id, eBay_User_Id__c From Case Where Item_Id__c IN : itemIds LIMIT 1000]); Map<ID,Contact> contactMap = new Map<ID,Contact>([Select Id, eBay_User_Id__c From Contact Where Id IN :contactIds LIMIT 1000]); for(Order_Line_Item__c o : trigger.new) { for(Case a : caseList) { String eBayUserId; Contact con; if(contactMap!=null) con = contactMap.get(o.Contact__c); if(con!=null) eBayUserId = con.eBay_User_Id__c; if(a.Item_Id__c == o.Marketplace_Item_ID__c && a.eBay_User_Id__c == eBayUserId) { a.Related_To_Order__c = o.Order__c; a.Related_To_Order_Line__c = o.Id; a.SKU__c = o.Product_Code__c; a.ContactId = o.Contact__c; casesToUpdate.add(a); } } } update casesToUpdate; }

 

 

 

CRM JediCRM Jedi

My guess of the cause is the following portion of your code:

 

   for(Order_Line_Item__c o : trigger.new) {
for(Case a : caseList) {
String eBayUserId;
Contact con;
if(contactMap!=null) con = contactMap.get(o.Contact__c);
if(con!=null) eBayUserId = con.eBay_User_Id__c;
if(a.Item_Id__c == o.Marketplace_Item_ID__c && a.eBay_User_Id__c == eBayUserId) {
a.Related_To_Order__c = o.Order__c;
a.Related_To_Order_Line__c = o.Id;
a.SKU__c = o.Product_Code__c;
a.ContactId = o.Contact__c;
casesToUpdate.add(a);
}
}
}

 

 

If you have 10 script statements in a for-loop, and you loop it 10 times, the total script statements is 10x10 which is 100 statements.

 

Assuming you have 1000 records in your inner loop (caseList).Thats 10*1000 records, which is 10000 lines and that is already hitting the LIMIT of executed script statements (without scaling).

 

Now multiply by the number of records in the outer loop (trigger.new) and see how many statements you need. :)