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
streetstreet 

How to fire the below trigger only once.

trigger updateaccountstatus on Opportunity (after update)
 {   
    for (Opportunity o : Trigger.new)
    {
    Opportunity opp =[select ID,Name, StageName,AccountId  from Opportunity where ID =:o.id];
        if (Trigger.isUpdate)
        {         
          account acct=[select ID,Name,Status__c from account where  id =:opp.AccountId];
           if(opp.StageName=='Suspend')
           {
          
           acct.Status__c='Suspend';
            update acct;  
                            
           }       
        }
      
    }
    
 }

 I want to fire the above trigger only once. 

Got to know that by using static variables we can fire trigger only once but how to implement in my above code.

Avi646Avi646

from opputunity trigger you are updating record of accounts so that wont call the oppurtunity trigger recirsively.

 

if you want to block recursing you can declare a static variable in a Apex class and check the value of the variable before doing any dml.

 

public class Recursion blocker{
   public static Boolean flag = true;
}

 code inside trigger

trigger updateaccountstatus on Opportunity (after update){
   if(RecursionBlock.Flag = true){
           RecursionBlock.Flag = false;
          //do your operation here
    }
}

 

streetstreet
trigger updateaccountstatus1 on Opportunity (before update)
 {   
    for (Opportunity o : Trigger.new)
    {
    Opportunity opp =[select ID,Account_stage_updated__c,Name, StageName,AccountId  from Opportunity where ID =:o.id];
        if (Trigger.isUpdate)
        { 
         if(!once.firstRun)
         {  
         system.debug('Firstrun.............'+once.firstRun);           
          account acct=[select ID,Name,Status__c from account where  id =:opp.AccountId];
           if(opp.StageName=='Suspend') 
           {          
           acct.Status__c='Suspend';
           update acct;  
           once.firstRun=true;
             system.debug('Firstrun......last................'+once.firstRun);           
              // Trigger.new[0].addError('Before Account Delete Error');   
           } 
           }      
        }
      
    }
    
 }
 
 
 

 Here im looking for once the condition is met i.e oop.stagename=='suspend', the account status will update. next If the same oop.stagename=='suspend' trigger should not fire bez already once the criteria is met and account got update. So, again if at all the status of opportunity is same the trigger should not fire.

 

Is my above code wrks for it?

Avi646Avi646

trigger will always fire despite of the fact condiotion are met or not. But u can stop the updation of account record by checking the status field. You need the static variable only when there is a trigger in account object which again updates the oppurtunity object