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
Daniel B ProbertDaniel 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;
           }
      }
}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

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!

 

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);
}

Map<String,ID> desKeyToDesIdMap = new Map<String,ID> ();  // key = des.term__c +  des.Year__c + des.District__c + des.Sheet_Type__c
Map<String,ID> esKeyToEsIdMap = new Map<String,ID> ();  // key = es.term__c + e.Year__c + es.Year__c + e.School__c + es.School__c + es.Sheet_Type__c

for (District_Entitlement_Sheet__c des: [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()]
   desKeyToDesIdMap.put(des.term__c +  des.Year__c + des.District__c + des.Sheet_Type__c,des.id);


for (Entitlement_Sheet__c es : [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()] )
   esKeyToEsIdMap.put(es.term__c + es.Year__c +  es.School__c + es.Sheet_Type__c,es.id);


//Now that we have the maps populated of any potential matches iterate through trigger list and match; 
for(Entitlement__c e: trigger.new) {
      if (desKeyToDesIdMap.containsKey(e.Term__c + e.Year__c + e.DistrictID__c + e.StudentType__c)
          e.District_Entitlement_Sheet__c = desKeyToDesIdMap.get(e.Term__c + e.Year__c + e.DistrictID__c + e.StudentType__c);
           
      }
      if (esKeyToEsIdMap.containsKey(e.Term__c + e.Year__c + e.School__c + e.StudentType__c)
          e.Entitlement_Sheet__c = esKeyToEsIdMap.get(e.Term__c + e.Year__c + e.School__c + e.StudentType__c);
           
      }

}
}

 

All Answers

Mayank_JoshiMayank_Joshi
Hi,

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
crop1645crop1645

Here is possibly your issue:

 

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;
           }
      }

 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.

 

 

Daniel B ProbertDaniel B Probert
thanks for the reply there is only one other trigger that is running - the emails I mentioned are emails from the system telling me there has been an error with the trigger so more a system alert.

Daniel B ProbertDaniel B Probert
how would I go about doing that I'm new to this so still learning?
crop1645crop1645

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!

 

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);
}

Map<String,ID> desKeyToDesIdMap = new Map<String,ID> ();  // key = des.term__c +  des.Year__c + des.District__c + des.Sheet_Type__c
Map<String,ID> esKeyToEsIdMap = new Map<String,ID> ();  // key = es.term__c + e.Year__c + es.Year__c + e.School__c + es.School__c + es.Sheet_Type__c

for (District_Entitlement_Sheet__c des: [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()]
   desKeyToDesIdMap.put(des.term__c +  des.Year__c + des.District__c + des.Sheet_Type__c,des.id);


for (Entitlement_Sheet__c es : [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()] )
   esKeyToEsIdMap.put(es.term__c + es.Year__c +  es.School__c + es.Sheet_Type__c,es.id);


//Now that we have the maps populated of any potential matches iterate through trigger list and match; 
for(Entitlement__c e: trigger.new) {
      if (desKeyToDesIdMap.containsKey(e.Term__c + e.Year__c + e.DistrictID__c + e.StudentType__c)
          e.District_Entitlement_Sheet__c = desKeyToDesIdMap.get(e.Term__c + e.Year__c + e.DistrictID__c + e.StudentType__c);
           
      }
      if (esKeyToEsIdMap.containsKey(e.Term__c + e.Year__c + e.School__c + e.StudentType__c)
          e.Entitlement_Sheet__c = esKeyToEsIdMap.get(e.Term__c + e.Year__c + e.School__c + e.StudentType__c);
           
      }

}
}

 

This was selected as the best answer
Daniel B ProbertDaniel B Probert

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

 

 

 

 

Daniel B ProbertDaniel B Probert

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 :)

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);
}
Map<String,ID> desKeyToDesIdMap = new Map<String,ID> ();
Map<String,ID> esKeyToEsIdMap = new Map<String,ID> ();
for (District_Entitlement_Sheet__c des: [SELECT Id, Term__c, Year__c, DistrictID__c, Sheet_Type__c FROM District_Entitlement_Sheet__c WHERE Term__c in: TermsMap.KeySet() OR Year__c in: YearsMap.keySet() OR DistrictID__c in: DistrictsMap.KeySet() OR Sheet_Type__c in: RecordTypeMap.KeySet()])
    desKeyToDesIdMap.put(des.term__c + des.Year__c + des.DistrictID__c + des.Sheet_Type__c,des.id);
for (Entitlement_Sheet__c es : [SELECT Id, Term__c, Year__c, SchoolID__c, Sheet_Type__c FROM Entitlement_Sheet__c WHERE Term__c in: TermsMap.keySet() OR Year__c in: YearsMap.KeySet() OR SchoolID__c in: SchoolsMap.KeySet() OR Sheet_Type__c in: RecordTypeMap.KeySet()] )
    esKeyToEsIdMap.put(es.term__c + es.Year__c + es.SchoolID__c + es.Sheet_Type__c,es.id);
for(Entitlement__c e: trigger.new) {
    if (desKeyToDesIdMap.containsKey(e.Term__c + e.Year__c + e.DistrictID__c + e.StudentType__c))
        e.District_Entitlement_Sheet__c = desKeyToDesIdMap.get(e.Term__c + e.Year__c + e.DistrictID__c + e.StudentType__c);
    if (esKeyToEsIdMap.containsKey(e.Term__c + e.Year__c + e.School__c + e.StudentType__c))
        e.Entitlement_Sheet__c = esKeyToEsIdMap.get(e.Term__c + e.Year__c + e.School__c + e.StudentType__c);
    }
}