4

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.

1
  • Can you post the complete code for class B? Also if a batch class invoked class B method and class B method invokes future method then that is not allowed.
    – javanoob
    Apr 2, 2016 at 13:30

1 Answer 1

7

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

3
  • 4
    More importantly, he calls class A (future method), from a method called while was already in future (th same method in Class A). That explains the error. Apr 2, 2016 at 15:12
  • Very true ..it is due to recursion. Removed @future call and made method as static .It iss working fine..Thanks all Apr 4, 2016 at 12:24
  • Making synchronous calls Apr 4, 2016 at 12:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .