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

I have two classes class A and class B, I am trying to call Class A's future method from class B's method and I get this error :'Future method cannot be called from a future or batch method'.

Class A:
This my future method :

public static account refAccount;
@future (callout= true )  
public static void getID(id AccId)
{
system.debug('New Id ' +AccId);    
 if(AccId != null)
 {
  refAccount = [SELECT Id,Name FROM Account WHERE Id = :AccId LIMIT 1];
       }    
   ClassA A=new ClassA();
   A.Refresh();// Calling method from same class .
}
Public void Refresh()  {
for (Item a:accountArray){
if(a.customer_code!=refAccount.Customer_ID__c)
{
ClassB b=new ClassB();   
b.processResponse(jsonstring);   // Here the method of Class B is called                 
}                  
}

Class B's method call the future method as follows:

Class B

Public void processResponse(String jsonString) {
Item[] items = (Item[]) System.JSON.deserialize(jsonString, Item[].class); 
Account[] sobs = new Account[] {};
for (Item item : items) {
sobs.add(convert(item));
}
 Database.SaveResult[] srList = Database.insert(sobs, false);
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {   
Batch_helper.getID(sr.getId()); //From Here I am calling class A's FutureMethod.
}
}
}

Not sure why I am getting error.

Best Answer

As the error states you are calling a future method from another future method .

Your class A calls Class B but I see again you call Class A methods in Class B causing recursion .

Hence would suggest to keep only one class and do all future operations in a single future class

Related Topic