8

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 ?

3
  • 1
    I have an open question about this myself. The problem seems to be that you can't chain Queueable and callout at the same time. I'm not sure if this is documented, but it seems you should simply use batchable instead.
    – sfdcfox
    Jul 27, 2015 at 12:54
  • Hi @sfdcfox I'm having this same issue. Since your comment seems to make the most sense and to be the only to directly referencing the question, I was wondering if you have any new information on whether using a batchable instead of queueable allows it to be called multiple time from a future(callout=true) class? I'd also appreciate if you post the link to your question. Thanks much!
    – Bahman.A
    Oct 20, 2020 at 3:58
  • 1
    @Bahman.A You may want to ask a new question. In my case, it was due to a bug that was fixed. See my question and the answer I accepted. I haven't had a problem with them since.
    – sfdcfox
    Oct 20, 2020 at 4:38

2 Answers 2

5

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

4
  • I have updated the Question with the code I am trying to execute.
    – S.Sharma
    Jul 27, 2015 at 9:49
  • 2
    Got it. 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.
    – SEuser
    Jul 27, 2015 at 9:52
  • 1
    Actually this error "Too many queueable jobs added to the queue: 2" is only occurring when I am trying to call the Queueable interface code from the future method. If I directly call it from developer console, it doesn't give me any error.
    – S.Sharma
    Jul 27, 2015 at 11:30
  • 1
    Guys, I have the same problem. It looks like if queueqble called from async method (future or batch chunk) it has such limit. Is there any method to check the context of execution (immediate or async ) in order to prevent invocation of queueable? Sep 12, 2015 at 13:30
3

It's right there in the documentation. You can't call multiple queueable from queueable.

When chaining jobs, you can add only one job from an executing job with System.enqueueJob, which means that only one child job can exist for each parent queueable job. Starting multiple child jobs from the same queueable job isn’t supported.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm#!

You must log in to answer this question.

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