You need to sign in to do that
Don't have an account?
Daniel B Probert
System.LimitException: Too many code statements: 200001
Whenever I do bulk api uploads to the object referenced in this code I receive an hundreds of emails and failures with the error:
System.LimitException: Too many code statements: 200001
any ideas how I can limit/update this apex to prevent this error and allow me once again to upload in bulk.
Trigger entitlementTrigger on Entitlement__c (before insert, before update) { Map<String, String> YearsMap = new Map<String, String>(); Map<String, String> TermsMap = new Map<String, String>(); Map<String, String> DistrictsMap = new Map<String, String>(); Map<String, String> SchoolsMap = new Map<String, String>(); Map<String, String> RecordTypeMap = new Map<String, String>(); for(Entitlement__c e: trigger.new) { YearsMap.put(e.Year__c, null); TermsMap.put(e.Term__c, null); DistrictsMap.put(e.DistrictID__c, null); SchoolsMap.put(e.School__c, null); RecordTypeMap.put(e.StudentType__c, null); } List<District_Entitlement_Sheet__c> DESList = [SELECT Id, Term__c, Year__c, District__c, Sheet_Type__c FROM District_Entitlement_Sheet__c WHERE Term__c in: TermsMap.KeySet() OR Year__c in: YearsMap.keySet() OR District__c in: DistrictsMap.KeySet() OR Sheet_Type__c in: RecordTypeMap.KeySet()]; List<Entitlement_Sheet__c> ESList = [SELECT Id, Term__c, Year__c, School__c, Sheet_Type__c FROM Entitlement_Sheet__c WHERE Term__c in: TermsMap.keySet() OR Year__c in: YearsMap.KeySet() OR School__c in: SchoolsMap.KeySet() OR Sheet_Type__c in: RecordTypeMap.KeySet()]; //Now that we have the lists populated of any potential matches iterate through trigger list and match for(Entitlement__c e: trigger.new) { for(District_Entitlement_Sheet__c des: DESList){ if(e.Term__c == des.term__c && e.Year__c == des.Year__c && e.DistrictID__c == des.District__c && e.StudentType__c == des.Sheet_Type__c) { e.District_Entitlement_Sheet__c = des.Id; } } for(Entitlement_Sheet__c es: ESList){ if(e.Term__c == es.term__c && e.Year__c == es.Year__c && e.School__c == es.School__c && e.StudentType__c == es.Sheet_Type__c) { e.Entitlement_Sheet__c = es.Id; } } } }
Try something like this -- not verified for compiler errors - but you get the idea -- now your trigger will not loop through large sublists for each of the 200 triggered rows. Problem reduced from 200 x large + 200 x large to: 200 + 200 + 2 x 200 (more or less).
As the Brits would say - "and Bob's your uncle!"
All Answers
Is there any specific reason for using Maps..Map<String, String> YearsMap = new Map<String, String>();
Map<String, String> TermsMap = new Map<String, String>();
Map<String, String> DistrictsMap = new Map<String, String>();
Map<String, String> SchoolsMap = new Map<String, String>();
Map<String, String> RecordTypeMap = new Map<String, String>();
You can try to make use of List instead of map . Otherwise your trigger looks clean
Here is possibly your issue:
You are looping through every Entitlement__c (up to 200 in a batch) and then linear searching through every member of list DESList and linear searching through every member of ESList. If those lists are long (only a few hundred will do), you will have 200 x 'hundreds' = tens-to-hundreds of thousands of statements being executed.
Instead of building DESList and ESList, build a Map with a composite key = es_term__c + es_year__c + es_school__c + es.sheet_type__c. Then your main loop merely need ascertain if the composite key built from the corresponding e. fields exists in the map; if yes, you get the es.id as the map's value. Same principle for the DESList
You also mentioned emails being sent which implies either workflows firing or other apex classes sending emails. If there are other triggers firing as part of your transaction, their script statements will get counted in the total.
Try something like this -- not verified for compiler errors - but you get the idea -- now your trigger will not loop through large sublists for each of the 200 triggered rows. Problem reduced from 200 x large + 200 x large to: 200 + 200 + 2 x 200 (more or less).
As the Brits would say - "and Bob's your uncle!"
Hi Sorry for the delay responding to this, I've been testing this and can't get the new trigger to work.
when I add the original trigger I receive this error:
Error: Compile Error: expecting a right parentheses, found 'desKeyToDesIdMap.put' at line 19 column 3
and so I add a ) at the end of line 19 and then I receive:
Error: expecting a right parentheses, found 'e.District_Entitlement_Sheet__c' at line 29 column 10
I then remove the } from line 31 and am able to save the trigger.
then when i test the trigger nothing happens i.e. the mapping no longer happens?
Any ideas?
Cheers
Dan
hi there ignore my previous message I had to slightly rework what you sent me to get it working but the code you've given me was perfect.
First thing I needed to do was create a new field called DistrictID__c which is formula just showing the id on the district entitlement sheet, then I had to do the same for the other with schoolid__c seem that the mapping was failing on the ID lookup.
As soon as I updated the script with the 2 new fields it started working - going to deploy it into production now.
thank you so much for pointing me in the right direction appreciate :)