+ Start a Discussion
krishna23krishna23 

While insert new account records before update account trigger is firing.

While inserting new account records before update account trigger is firing. I don't want to execute update trigger how to stop these update trigger execution. Please assist me.

Thanks!
Best Answer chosen by krishna23
Khan AnasKhan Anas (Salesforce Developers) 
Hi Krishna,

We can use a static variable to prevent code to be executed. As per the order of execution, all Before Trigger and After Trigger will fire before the workflow update. To prevent Trigger to be fired the second time, after workflow field update we can set a static boolean variable in a Class.

Create a static variable in a class as true. Make it false before or after a trigger has been executed. Variable will become false when trigger runs for the first time (before workflow update). If the variable is true only then trigger will fire and if the trigger will try to execute the second time then it will not fire.

Handler Class:
public class TestHandler{
     public static Boolean isTriggerExecuted = true;
}

Trigger:
trigger testTrigger on Account (before update){
     
    if(TestHandler.isTriggerExecuted){
         
        //Logic

        TestHandler.isTriggerExecuted = false;
    }
}


I hope it helps you.

Kindly mark this as solved if the information was helpful.

Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Krishna,

I trust you are doing very well.

You might have other triggers on Account object that cause an update after a record is inserted.
Or maybe there's workflow on the Account object that causes a field update. That will, in turn, trigger the update trigger. 

Also, you can refer below link and check sfdcfox answer for recursive trigger firing which might help you further with the above issue.

https://developer.salesforce.com/forums/?id=906F00000008xGgIAI


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
krishna23krishna23
Hi Anas,

Thanks for the quick response. Exactly because of workflow field update account update trigger firing. As per my knowledge, we can use recursive to avoid trigger multiple executions. But here we are trying to stop first time also while inserting. Is it possible to stop update trigger after workflow field update?
Niraj Kr SinghNiraj Kr Singh
Hi Krishna,

Might be having Workflow which is updating you account when inserting account record. Check It.

Or
You can use context variables to controll the execution of your trigger according to the trigger.
Follow this link: https://trailhead.salesforce.com/en/modules/apex_triggers/units/apex_triggers_intro
Or
You can write a Recursionhandler Class, which will have a static boolean variable. Through that you can handle.
Follow this link: http://www.sfdcpoint.com/salesforce/avoid-recursive-trigger-salesforce/

thanks
Niraj
Khan AnasKhan Anas (Salesforce Developers) 
Hi Krishna,

We can use a static variable to prevent code to be executed. As per the order of execution, all Before Trigger and After Trigger will fire before the workflow update. To prevent Trigger to be fired the second time, after workflow field update we can set a static boolean variable in a Class.

Create a static variable in a class as true. Make it false before or after a trigger has been executed. Variable will become false when trigger runs for the first time (before workflow update). If the variable is true only then trigger will fire and if the trigger will try to execute the second time then it will not fire.

Handler Class:
public class TestHandler{
     public static Boolean isTriggerExecuted = true;
}

Trigger:
trigger testTrigger on Account (before update){
     
    if(TestHandler.isTriggerExecuted){
         
        //Logic

        TestHandler.isTriggerExecuted = false;
    }
}


I hope it helps you.

Kindly mark this as solved if the information was helpful.

Regards,
Khan Anas
This was selected as the best answer
krishna23krishna23
So you mean to say we can stop the second time only. We can not stop 3 step first time while inserting the record.
1.Account creation
2.workflow field update
3.update trigger execution 

Thanks,
Kishore