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 ?