Future and queueable are both async process as per the documentation, And async can't be called from the async process. Then why when we try to reference a future method inside a Queueable class, it compiles successfully.Please find the code below :

public class AddPrimaryContactRev implements Queueable {
    Contact con;
    String strState;
    public AddPrimaryContactRev (Contact con, String strState) {
        this.con = con;
        this.strState = strState;

    public void execute(QueueableContext qc) {
        List < Account > listAccount = [ SELECT Id FROM Account WHERE BillingState =: strState LIMIT 100];
        List < Contact > listContact = new List < Contact >();
        for ( Account acc : listAccount ) {
            Contact c = con.clone();
            c.AccountId = acc.Id;
        insert listContact;
        set<Id> aid = new set<Id>();
        AccountProcessorFuture.countContacts(aid );


public class AccountProcessorFuture
  public static void countContacts(Set<id> setId) 
      List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
      for( Account acc : lstAccount )
          List<Contact> lstCont = acc.contacts ;

          acc.Number_of_Contacts__c = lstCont.size();
      update lstAccount;

There's no rule that says that "async cannot call async". There are specific rules in place, such as "future cannot call future," but these are specific rules called out in the documentation. You'll want to read the governor limits documentation, as well as that for Future methods, Queueable methods, Batchable methods, and Scheduleable methods.

For example, a Queueable can call another Queueable, a Batchable can call another Batchable in the finish method, and Scheduleable methods can call Batchable and Queueable methods. There are usually lesser limits allowed for asynchronous contexts (e.g. a Queueable can only one Queueable, while a normal synchronous transaction can call 50 of them).

Note that your specific example isn't exactly valid, either. The compiler can't distinguish between the execute method of a Batchable class and a normal method that happens to be called execute; since it is a normal method, you could call execute from somewhere else that would be legal for the future method to be called. Therefore, it can't block the call to a future method. However, if the method were not allowed, it would cause a run-time governor limit exception instead. The only method that can reliably be detected by the compiler (and is enforced as such) is the "future calling future" scenario.

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.