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
sai krishna 267sai krishna 267 

how can be avoid the recursive below trigger?

trigger beforeup on Account (before update) {
    for(Account acc:trigger.new){
    acc.name='123';
    }  
    update trigger.new;
}
Raj VakatiRaj Vakati
Refer this link 
https://help.salesforce.com/articleView?id=000133752&language=en_US&type=1​
 
trigger beforeup on Account (before update) {
if(checkRecursive.runOnce())
    {
    for(Account acc:trigger.new){
    acc.name='123';
    }  
    update trigger.new;
	}
}


public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Just update your code like below

trigger beforeup on Account (before update) {
    for(Account acc:trigger.new){
    acc.name='123';
    }  
}

But if you want to learn about Recursive Trigger please check below post
http://amitsalesforce.blogspot.com/2015/03/how-to-stop-recursive-trigger-in.html


Problem :-  

1) Many Developers face recursive trigger , or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.

2) You want to write a trigger that creates a new record ; however, that record may then cause another trigger to fire, which in turn causes another to fire, and so on. 
  
Solution :-

you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.

Apex Class with Static Variable
public class ContactTriggerHandler
{
     public static Boolean isFirstTime = true;
}



Trigger Code
trigger ContactTriggers on Contact (after update)
{
    Set<String> accIdSet = new Set<String>(); 
    if(ContactTriggerHandler.isFirstTime)
    {
        ContactTriggerHandler.isFirstTime = false;
         for(Contact conObj : Trigger.New)
  {
            if(conObj.name != 'Test') 
     {
                accIdSet.add(conObj.accountId);
            }
         }
   // any code here
    }
}

Let us know if this will help you