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.

  • 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 '16 at 13:30
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

  • 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. – Sebastian Kessel Apr 2 '16 at 15:12
  • Very true ..it is due to recursion. Removed @future call and made method as static .It iss working fine..Thanks all – Salesforce Learner Apr 4 '16 at 12:24
  • Making synchronous calls – Salesforce Learner Apr 4 '16 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.