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
Carolina W 2Carolina W 2 

Error: Future method cannot be called from a future or batch method

Hi! How can I fix this error?

 System.AsyncException: Future method cannot be called from a future or batch method

I guess this happen because my future method is an update and the Trigger is called in looping. So I created the boolean runTrigger to not run the trigger when it's false, but I don't know how to do it exactly.

P.s: It need to be a future method

Class

public class AccountUpdate {
    public static boolean runTrigger = true; //I create this variable to stop the looping, but I don't know where I have to put 'runTrigger = False;'
    
    //This future Mehod update the Account Name 
    @Future
	 public static void doAutomaticExternIntegration(List<Id> ids) {
         
         List<Account> accountsFound = [SELECT ID, Name FROM Account WHERE Id IN :ids];
        
        List<Account> accountToUpdate = new List<Account>();
         
          for (Account acc: accountsFound){
              
              acc.Name = 'ABCD';
              
              accountToUpdate.add(acc);
          }
         
         update accountToUpdate;
     }
     
    //This class runs the Future Method above
    public static void runFutureMethod (List<Account> accs){
        
       if(!runTrigger){
            return;
       }else{
           
    	List<Id> accountIds = new List<Id>();
			for(Account acc : accs){
				AccountIds.add(acc.Id);
			}
           
       	doAutomaticExternIntegration(accountIds);
      }
    }
}
Trigger
trigger AccountTrigger on Account (after update) {
    
    AccountUpdate.runFutureMethod(trigger.new);
}

 
Best Answer chosen by Carolina W 2
Carolina W 2Carolina W 2

Thank you @Swetha !  The solution:

 

public class AccountUpdate {

  public static boolean runTrigger = true; 

  //This future Mehod update the Account Name
  @Future
  public static void MyFutureMethod(List<Id> ids) {
     runTrigger = false;
     List<Account> accountsFound = [SELECT ID, Name FROM Account WHERE Id IN :ids];
      List<Account> accountToUpdate = new List<Account>();
      for (Account acc: accountsFound){
         acc.Name = 'ABCD';
         accountToUpdate.add(acc);
       }
    update accountToUpdate;
  }

  //This class runs the Future Method above
  public static void runFutureMethod (List<Account> accs){
     if(runTrigger) {
       List<Id> accountIds = new List<Id>();
       for(Account acc : accs){
          AccountIds.add(acc.Id);
       }
      MyFutureMethod(accountIds);
    }
  }
}

All Answers

SwethaSwetha (Salesforce Developers) 
Hi Carolina,
Does the example code mentioned in the post https://salesforce.stackexchange.com/questions/102196/error-future-method-cannot-be-called-from-a-future-or-batch-class/102200 help?

If this information helps, please mark the answer as best.Thank you
Carolina W 2Carolina W 2

Thank you @Swetha !  The solution:

 

public class AccountUpdate {

  public static boolean runTrigger = true; 

  //This future Mehod update the Account Name
  @Future
  public static void MyFutureMethod(List<Id> ids) {
     runTrigger = false;
     List<Account> accountsFound = [SELECT ID, Name FROM Account WHERE Id IN :ids];
      List<Account> accountToUpdate = new List<Account>();
      for (Account acc: accountsFound){
         acc.Name = 'ABCD';
         accountToUpdate.add(acc);
       }
    update accountToUpdate;
  }

  //This class runs the Future Method above
  public static void runFutureMethod (List<Account> accs){
     if(runTrigger) {
       List<Id> accountIds = new List<Id>();
       for(Account acc : accs){
          AccountIds.add(acc.Id);
       }
      MyFutureMethod(accountIds);
    }
  }
}
This was selected as the best answer