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
Justin Kitagawa 6Justin Kitagawa 6 

Avoiding recursive Future Calls

I am working on a project where I am hoping to do the following: 

On Lead Insert --> Trigger to call @future callout method to get append information on the lead. 
Query Lead database to find if other leads exist for that company (using email domain)
    --> If New Company 
          --> call @future callout method to search for additional leads and bring them in as leads. 
                --> call @future callout method to append information to these leads. 

But I am running into an issue as my Update call is triggering my insert call, which is then triggering the update call again and I am getting an error as a future method cannot be invoked by another future method... 

I am thinking of either: 
  1. Making the second API call append data to the newly created additional leads included in the method to insert them in the first place 
  2. Making the call to get additional leads scheduled instead. 
Im sure there is a way around this... has anybody done something similar in the past? 

Any help would be greatly appreciated 
CloudGeekCloudGeek
Hello Justin,

You may want to try using below approach :

Suppose that you had the following class.
public class P { 
   public static boolean firstRun = true; 
}

A trigger that uses this class could then selectively fail the first run of the trigger.
 
trigger T1 on Account (before delete, after delete, after undelete) { 
       if(Trigger.isBefore){
          if(Trigger.isDelete){
             if(p.firstRun){
                 Trigger.old[0].addError('Before Account Delete Error');
                  p.firstRun=false;
              } 
           }
        }
}

For a deep dive :  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm