0

Good day Everyone,

I have a trigger that will call a Queueable. The process looks like this :

Trigger :
for(User userData : Trigger.new) {
if(Limits.getLimitQueueableJobs() - Limits.getQueueableJobs() > 0 && !Test.isRunningTest()) { 
    system.enqueueJob(new Assignment(userData)); 
} else {
    System.debug('FUTURE LIMIT EXCEEDED');
}

}

and my Queueable looks like this :

public class Assignment implements Queueable, Database.AllowsCallouts {
    // the constructor is parameter is used in execute method
    public Assignment(User theUser) {   
    }

    public void execute(QueueableContext context) {
        // some process here
    }
}

At the moment everything is working fine but what i'm trying to achieve is that the next Queueable will only run if the first Queueable is done running. What I am trying to achieve is something like a chaining Queueable. I did try to update my trigger to only call the Queueable once then I update my Queueable class to something like this :

    Trigger : 
system.enqueueJob(new Assignment(Trigger.new)); 


Queueable:
public class Assignment implements Queueable, Database.AllowsCallouts {
    public List<User> allUserList = new List<User>();

    // the constructor is parameter is used in execute method
    public Assignment(List<User> userList) {   
        allUserList = userList;
    }

    public void execute(QueueableContext context) {
        // some process here



        // at the end of the execute method
        allUserList.remove(0);
        if(allUserList.size() > 0) {
            system.enqueueJob(new Assignment(allUserList)); 
        }
    }
}

But when I update many data simultaneously, I encounter this error.

Too many queueable jobs added to the queue

Can you please give your idea what do I need, to do this ? Thank you.

  • You've oversimplified your code. You are allowed to chain once per execute method, but without knowing more about the details of what's going on, we're not going to be able to help you. You don't need to publish your actual live code, but you do need something more realistic. – sfdcfox Jan 9 at 4:45
  • Also, where are you getting this error? In a unit test? Execute anonymous? During an API call or Visualforce page? etc... The more we know, the better we can help you. – sfdcfox Jan 9 at 4:47
  • @sfdcfox I have made an update on my post, and I encounter the error if I modify many data simultaneously. – Hope Jan 9 at 5:03
  • 1
    Okay, theory: you have a trigger or something else that tries to enqueue another job before yours gets to, thus resulting in the error. You might need to hunt it down and prevent it from doing so when already in queueable context or do some redesign. – sfdcfox Jan 10 at 6:19
  • @sfdcfox Yeah you are right mate. I tried to look at the debug log, and found that something else tries to enqueue another job that is why I tried to change this to batch as per my new question which you commented also. Thanks. – Hope Jan 10 at 6:26

Your Answer

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

Browse other questions tagged or ask your own question.