[SalesForce] Call Queueable from future method

I have a use-case where I need to execute some logic in async mode and I found that using Queueable Interface I can achieve that. But when I am trying to the use the Queuable Interface, some issues are occurring with the Queueable feature calls.

1) When I am trying to enqueue the same class for multiple time, then it's throwing below error :

Maximum stack depth has been reached.

2) When I change the logic, and tried to execute the Enqueue job from a future method, then it started throwing below error :

First error: Too many queueable jobs added to the queue: 2  

Below is the code I am trying to execute in Queueable Class:

public class SecondQueueableApex implements Queueable {

public Integer counter;

public SecondQueueableApex (Integer counter) {
   this.counter= counter;
}

public void execute(QueueableContext context) {
    Account a = new Account(Name='Aaccount'+counter,Phone='(415) 555-1212');
    insert a;      
}

}

@future(callout=true)
public static void executeFuture() {
    for(Integer counter=1;counter<10;counter++) {
        System.enqueueJob(new SecondQueueableApex(counter));
    }

}

These limitations are not mentioned anywhere in the salesforce documentation. Can anyone please help me understanding these issues ?

Best Answer

This will actually a bug in the code.

I can see this line -

System.enqueueJob(new SecondQueueableApex(counter));  

I assume this is calling itself, due to which it starts getting into stack.

This will not be limitation of Queueable interface, but more about the how it is being called.

Current limit for number of such chained jobs can be 5. Check "Queueable Apex Limits" in this reference

If you can add the complete class of Queueable interface, I can check further if still facing issues.

Update 1 From comment -

Initially Sf added this limitation where you can have maximum of 2 Queueable jobs. This limitation has been removed now. I strongly feel that this is because of your org not having proper updated Queueable interface features. Try logging a case or trying on an org from different continent. So suppose if you are on ap1 POD, some how try on a org with different POD like eu1 or na1 etc

Do let me know if it helps .

Thanks,

Ray

Related Topic