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
jameskCAjameskCA 

Trigger is running multiple times on a new record - how to debug?

We have a trigger that runs on the lead before insert and before update.  Depending on whether it's a new record or updated record, it updates the lead owner differently.  I recently found out that it was running multiple times on each save of a record (or creation).  I'm trying to figure out why but I'm very new to apex and trying to figure out the best way to debug this.

We have a few managed packages that run on the lead as well and one other apex trigger that runs on after update but I'm not sure how to figure out what may be causing this particular trigger to run more than once.

I was able to determine it is running so many times with some debug statements, creating a new record, and seeing the debug statement 5 times (when it should only run once).  

Thanks in advance.  
v varaprasadv varaprasad
Hi ,

Just to stop recursion in trigger use below Syntax.

1.create sample static class with set of string.
 
public class RecursiveCheck {
    public static Set<String> triggerMonitor = new Set<String>();    
}



trigger ContactTrigger on Contact (after insert,before insert) {
    if(trigger.isInsert && !RecursiveCheck.triggerMonitor.contains('NAV_CRM_ContactTrigger')){
        RecursiveCheck.triggerMonitor.add('ContactTrigger');   //Here we need to add trigger name.
     
        ContactHandler.insertContactsToCISWS(trigger.new,inserts);
       
    }
}




where ever your doing DML in class just add below it will stop recursive.

if(updContacts.size() > 0){
                RecursiveCheck.triggerMonitor.add('ContactTrigger');   //Here we need to add trigger name.
                database.update(updContacts);
            }

Please check once and let me know if you need ny info.


Thanks
Varaprasad
Amit Chaudhary 8Amit Chaudhary 8
Please post your code so that we can help you

You can also check below post to stop the recursive Trigger issue
1) http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework

1) One Trigger Per Object
A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

2) Logic-less Triggers
If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org. 

3) Context-Specific Handler Methods
Create context-specific handler methods in Trigger handlers

4) Bulkify your Code
Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

5) Avoid SOQL Queries or DML statements inside FOR Loops
An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

6) Using Collections, Streamlining Queries, and Efficient For Loops
It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

7) Querying Large Data Sets
The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

8) Use @future Appropriately
It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found

9) Avoid Hardcoding IDs
When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail

http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

Let us know if this will help you
 
jameskCAjameskCA
Thank you both for the feedback.  In this case, it looks like some processes (process  builder) are causing the trigger to execute multiple times.  In a sandbox, I went through and disabled every apex trigger that ran on lead (other than the one I'm troubleshooting) and every installed app that runs on the lead.  I then started disabling processes and narrowed it down to two simple processes.

They are both very simple, checking some field values on the lead as the criteria, then updating a single field on the same record.

Any ideas why a process would cause the trigger to run multiple times?

I checked both processes and neither have the recursion checkbox checked.
murali ch 7murali ch 7
please help me.........................................................

A trigger is running multiple times during a single save event in an org. How can this be
prevented?
Ans: In the trigger, before executing your code keep a check that the variable is true or not.
Once you check, make the variable false.
Class code
public Class checkRecursive{
private static boolean run = true;
public static boolean runOnce(){
if(run){
run=false;
return true;
}else{
return run;
}
}
}
Trigger code
trigger updateTrigger on anyObject(after update) {
if(checkRecursive.runOnce())
{
//write your code here
}
}