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
Suresh RaghuramSuresh Raghuram 

Trigger: System.LimitException: Too many code statements: 200001

 

I tried all way to reduce the number of for loops but I did not a way to reduce them . any ideas is greatly appriciated.

List<ContactCode__c> contCodes=new List<ContactCode__c>();
List<ContactCode__c> delContCodes=new List<ContactCode__c>();
Map<String,List<Contact>> accContMap=new Map<String,List<Contact>>();
Map<String,Map<String,ContactCodeDescription__c>> codeAndCodeCategory=new Map<String,Map<String,ContactCodeDescription__c>>();
for(ContactCodeDescription__c cd:[select code_Type__c,Code__c from ContactCodeDescription__c]){
if(codeAndCodeCategory!=null && codeAndCodeCategory.get(cd.Code_Type__c)!=null &&                                          codeAndCodeCategory.get(cd.Code_Type__c).get(cd.Code__c)==null){                                                 
codeAndCodeCategory.get(cd.Code_Type__c).put(cd.Code__c,cd);                                  
}else{
Map<String,ContactCodeDescription__c> ccdMap=new Map<String,ContactCodeDescription__c>();
ccdMap.put(cd.Code__c,cd);
codeAndCodeCategory.put(cd.Code_Type__c,ccdMap);
}
}
for(Contact contact :[Select Id,AccountId, (Select Id, Category__c, Code__c from Related_Contacts__r) From Contact where AccountId IN: oldMap.keyset()]){
if(accContMap!=null && accContMap.get(contact.AccountId)!=null){
accContMap.get(contact.AccountId ).add(contact);
}else{
accContMap.put(contact.AccountId ,new List<Contact>{contact});
}
}


for(Account act:account){
if(oldMap.get(act.Id).Account_Type__c != act.Account_Type__c){
if(act.Account_Type__c.contains('Listed Company')){
for(Contact cnt:accContMap.get(act.Id)){
for(Contactcode__c cntCode:cnt.Related_Contacts__r){

Contactcode__c updCntCode=new Contactcode__c(Id=cntCode.Id);

if(codeAndCodeCategory!=null && codeAndCodeCategory.get('LC')!=null && codeAndCodeCategory.get('LC').get(cntCode.Code__c)!=null){
updCntCode.Code_Description__c=codeAndCodeCategory.get('LC').get(cntCode.Code__c).Id;
}else if(codeAndCodeCategory!=null && codeAndCodeCategory.get('L1')!=null && codeAndCodeCategory.get('L1').get(cntCode.Code__c)!=null){
updCntCode.Code_Description__c=codeAndCodeCategory.get('L1').get(cntCode.Code__c).Id;
}else{
delContCodes.add(updCntCode);
}
contCodes.add(updCntCode);
}

:) :) :) :) :):) :) :) :) :)

what are you trying to accomplish?

Suresh RaghuramSuresh Raghuram

There are  Accounts with Related Contacts and each related contact may have a contactCode. there is contact code description object .The contact code object is a junction obj between Contact and the Contact code description.

 

so when a field on the Account changes trigger fires on after update .I pull all the codes related to the code type(depends on the value in a field on account ) .If the code exist in the new code type(checks in code description obj) i will update the code type if not deletes the code completly from the Contact Code .

 

code and Code Type are formula fields on ContactCode which will be populated based on the look up to(code description).

 

if you still need more info please do not hesitate to ask.