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
ThronThron 

Strange too many code statements issue.

Hello, I'm having a strange issue where I'm getting too many code statements: 20001 on this trigger:

 


trigger UpdateLeadPass on Lead (before update) {
    
    Lead[] lead = Trigger.new;
    Lead[] oldLead = Trigger.old;

    if(Trigger.isUpdate) {    
        if (lead != null && oldLead != null){
            for(Lead leadPointer : lead) {
                for (Lead oldPointer : oldLead) {
                    if (leadPointer.bdr_to_sr__c == True){
                        leadPointer.bdr_to_sr_rep__c = UserInfo.getUserId();
                        leadPointer.Transfer_Date__c = System.Now();
                    } else if (oldPointer.bdr_to_sr__c == True && leadPointer.bdr_to_sr__c == false) {
                        leadPointer.bdr_to_sr_rep__c = null;
                    }
                    if (leadPointer.bdr_to_ae__c == True){
                        leadPointer.bdr_to_ae_rep__c = UserInfo.getUserId();
                        leadPointer.Transfer_Date__c = System.Now();
                    } else if (oldPointer.bdr_to_ae__c == True && leadPointer.bdr_to_ae__c == false) {
                        leadPointer.bdr_to_ae_rep__c = null;
                    }
                    if (leadPointer.bdr_to_reseller__c == True){
                        leadPointer.bdr_to_reseller_rep__c = UserInfo.getUserId();
                        leadPointer.Transfer_Date__c = System.Now();
                    } else if (oldPointer.bdr_to_reseller__c == True && leadPointer.bdr_to_reseller__c == false) {
                        leadPointer.bdr_to_reseller_rep__c = null;
                    }
                    if (leadPointer.sr_to_ae__c == True){
                        leadPointer.sr_to_ae_rep__c = UserInfo.getUserId();
                        leadPointer.Transfer_Date__c = System.Now();
                    } else if (oldPointer.sr_to_ae__c == True && leadPointer.sr_to_ae__c == false) {
                        leadPointer.sr_to_ae_rep__c = null;
                    }
                    if (leadPointer.sr_to_reseller__c == True){
                        leadPointer.sr_to_reseller_rep__c = UserInfo.getUserId();
                        leadPointer.Transfer_Date__c = System.Now();
                    } else if (oldPointer.sr_to_reseller__c == True && leadPointer.sr_to_reseller__c == false){
                        leadPointer.sr_to_reseller_rep__c = null;
                    }
                    if (leadPointer.ae_to_reseller__c == True){
                        leadPointer.ae_to_reseller_rep__c = UserInfo.getUserId();
                        leadPointer.Transfer_Date__c = System.Now();
                    } else if (oldPointer.ae_to_reseller__c == True && leadPointer.sr_to_reseller__c == false) {
                        leadPointer.ae_to_reseller_rep__c = null;
                    }
                }
            }    
        }
    }
}

 

 

The Trigger is very simple so I don't understand where this is happening? Strange thing as well, if I load let's say 350 leads the first 200 will get this error and the next 150 will load fine. If I reduce that too 300 leads loaded the first 200 still error and the next 100 make it in just fine.

 

Any help is very appreciated thank you!

 

-Jon

Best Answer chosen by Admin (Salesforce Developers) 
Grazitti InteractiveGrazitti Interactive

Hi,

 

I am not able to find why you are using nested for loop, if you want just to compare new version with older version of record then use use Trigger.oldMap rather than inner for loop as following:

 

trigger UpdateLeadPass on Lead (before update) {

Lead[] lead = Trigger.new;
Lead[] oldLead = Trigger.old;

if(Trigger.isUpdate) {
if (lead != null && oldLead != null){
for(Lead leadPointer : lead) {
//for (Lead oldPointer : oldLead) {
if(Trigger.oldMap.containsKey(lead.Id)){
if (leadPointer.bdr_to_sr__c == True){
leadPointer.bdr_to_sr_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).bdr_to_sr__c == True && leadPointer.bdr_to_sr__c == false) {
leadPointer.bdr_to_sr_rep__c = null;
}
if (leadPointer.bdr_to_ae__c == True){
leadPointer.bdr_to_ae_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).bdr_to_ae__c == True && leadPointer.bdr_to_ae__c == false) {
leadPointer.bdr_to_ae_rep__c = null;
}
if (leadPointer.bdr_to_reseller__c == True){
leadPointer.bdr_to_reseller_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).bdr_to_reseller__c == True && leadPointer.bdr_to_reseller__c == false) {
leadPointer.bdr_to_reseller_rep__c = null;
}
if (leadPointer.sr_to_ae__c == True){
leadPointer.sr_to_ae_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).sr_to_ae__c == True && leadPointer.sr_to_ae__c == false) {
leadPointer.sr_to_ae_rep__c = null;
}
if (leadPointer.sr_to_reseller__c == True){
leadPointer.sr_to_reseller_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).sr_to_reseller__c == True && leadPointer.sr_to_reseller__c == false){
leadPointer.sr_to_reseller_rep__c = null;
}
if (leadPointer.ae_to_reseller__c == True){
leadPointer.ae_to_reseller_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).ae_to_reseller__c == True && leadPointer.sr_to_reseller__c == false) {
leadPointer.ae_to_reseller_rep__c = null;
}
}
}
}
}
}

 

/**If this post helps you then please mark it as a solution and don't forget to give me kudo's.***/

 

Thanks

www.grazitti.com

All Answers

Cloud CredenceCloud Credence

Hi,

 

There could be other triggers related to this object, those are also firing.

 

If you reduce the batch size to 50 , your problem will be resolved. In data loader batch size is available in settings tab.

 

Best Wishes,

ThronThron

Thanks for your reply. I had wondered this same thing, but when I take the code out of production the issue goes away so I feel like there is something going on with this trigger (which the error references.)

 

I would love to reduce the batch size but I should clarify, the issue is showing itself when an ETL process is automatically loading leads into our salesforce instance.

liron169liron169

Hi,

 

You have double loop, so in case of 200 records it is: 40,000 statement per line in the loop, so it seems reasonable to get this exception.
You can try and reduce the batch size, but it might occur also if you do bulk update from code.

 

In my opinion, you can implement the same process without code, only with workflow(s) rule and fields update.

 

for example (for the first condition in the trigger):
Define fields  Update:
1.Update lead.Transfer_Date__c     value: $System.OriginDateTime
2.Update lead.bdr_to_sr_rep__c    value: IF(lead.bdr_to_sr__c, $User.id, null)

Define Workflows on lead:
1.Criteria:     lead.bdr_to_sr__c==true
Action:        field update 1 + 2

2.Criteria:     lead.bdr_to_sr__c==false && IsChanged(lead.bdr_to_sr__c)
Action:     field update 2

Similarly, define fields update + workflows for the other conditions you have in the triggers.

Grazitti InteractiveGrazitti Interactive

Hi,

 

I am not able to find why you are using nested for loop, if you want just to compare new version with older version of record then use use Trigger.oldMap rather than inner for loop as following:

 

trigger UpdateLeadPass on Lead (before update) {

Lead[] lead = Trigger.new;
Lead[] oldLead = Trigger.old;

if(Trigger.isUpdate) {
if (lead != null && oldLead != null){
for(Lead leadPointer : lead) {
//for (Lead oldPointer : oldLead) {
if(Trigger.oldMap.containsKey(lead.Id)){
if (leadPointer.bdr_to_sr__c == True){
leadPointer.bdr_to_sr_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).bdr_to_sr__c == True && leadPointer.bdr_to_sr__c == false) {
leadPointer.bdr_to_sr_rep__c = null;
}
if (leadPointer.bdr_to_ae__c == True){
leadPointer.bdr_to_ae_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).bdr_to_ae__c == True && leadPointer.bdr_to_ae__c == false) {
leadPointer.bdr_to_ae_rep__c = null;
}
if (leadPointer.bdr_to_reseller__c == True){
leadPointer.bdr_to_reseller_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).bdr_to_reseller__c == True && leadPointer.bdr_to_reseller__c == false) {
leadPointer.bdr_to_reseller_rep__c = null;
}
if (leadPointer.sr_to_ae__c == True){
leadPointer.sr_to_ae_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).sr_to_ae__c == True && leadPointer.sr_to_ae__c == false) {
leadPointer.sr_to_ae_rep__c = null;
}
if (leadPointer.sr_to_reseller__c == True){
leadPointer.sr_to_reseller_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).sr_to_reseller__c == True && leadPointer.sr_to_reseller__c == false){
leadPointer.sr_to_reseller_rep__c = null;
}
if (leadPointer.ae_to_reseller__c == True){
leadPointer.ae_to_reseller_rep__c = UserInfo.getUserId();
leadPointer.Transfer_Date__c = System.Now();
} else if (Trigger.oldMap.get(lead.Id).ae_to_reseller__c == True && leadPointer.sr_to_reseller__c == false) {
leadPointer.ae_to_reseller_rep__c = null;
}
}
}
}
}
}

 

/**If this post helps you then please mark it as a solution and don't forget to give me kudo's.***/

 

Thanks

www.grazitti.com

This was selected as the best answer
ThronThron

Thank you, the nested for loop was the issue although I didn't use your code exactly you led me down the right path. I appreciate all the comments everyone thanks again!