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
MohammadMohammad 

future method can not be called from a future method

Hi All, I am a newbie to salesforce. my problem is : I am inserting a new Lead Source that is a related list of Lead. In my Lead Source object
there are two lookups, one on Contact and the other on Lead. When I insert a new Lead Source (by giving the Lead and Contact names in the
Lead Source object, one more Lead Source should be inserted (with the same Lead name and a Contact name that is present on the first selected Contact as a look up
on Contact). I am using after insert to insert this second Lead Source. In before insert i had used @future because of governer limits and i am using
@future in my class that is called by my after insert trigger. i am using a flag to prevent execution of before insert second time. but i think, i am not able to prevent it
from execution as it gives an error "future method can not be called from a future method:methodName()". How can i check this. pls help.

Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

Create a global variable in the future class that is set to true when the @future methods are called.  Wrap the trigger's future method call in an If statement conditional upon that global variable.  That way you can't get the infinite loop error:

 

Trigger xyz on Object (after update){

 

If (futureMethodAlreadyCalled ==False ){

    doFutureMethod();

}

 

}

All Answers

jkucerajkucera

Create a global variable in the future class that is set to true when the @future methods are called.  Wrap the trigger's future method call in an If statement conditional upon that global variable.  That way you can't get the infinite loop error:

 

Trigger xyz on Object (after update){

 

If (futureMethodAlreadyCalled ==False ){

    doFutureMethod();

}

 

}

This was selected as the best answer
MohammadMohammad
Hi John, Thanks a lot. It really worked :)