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
Pranav S SanvatsarkarPranav S Sanvatsarkar 

How to avoid recursion of triggers in asynchronous apex?

Hello there,

I have triggers on Contact and User objects. Contact trigger updates user record in future method. The user trigger on the other hand updates related contact in future method.

As future method is an asynchronous apex, I cannot track the execution using static variable. Is there any standard way that my scenario can be handled?

Thanks in advance.
Best Answer chosen by Pranav S Sanvatsarkar
Alexander TsitsuraAlexander Tsitsura
Hello Pranav,

Running the future method from another method is impossible. As per Salesforce documentation,
You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.
Please, check the "Apex Jobs" logs(Setup -> Monitor -> Jobs -> Apex Jobs) for any errors. 

For solve the problem you can try to add a condition.
if (false == System.isFuture()) {
    // call your future method
}
If it helped you, then please select it the best answers it will help outer also.

Thank,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hello Pranav,

Running the future method from another method is impossible. As per Salesforce documentation,
You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.
Please, check the "Apex Jobs" logs(Setup -> Monitor -> Jobs -> Apex Jobs) for any errors. 

For solve the problem you can try to add a condition.
if (false == System.isFuture()) {
    // call your future method
}
If it helped you, then please select it the best answers it will help outer also.

Thank,
Alex
This was selected as the best answer
Pranav S SanvatsarkarPranav S Sanvatsarkar
Thanks Alexander. The solution is not the perfect fit but it helped me find a solution for my exact situation. Thanks again!