1

I am trying to update user records using batch and I am getting this error

Too many queueable jobs added to the queue: 2

I found out that the batch update is triggering triggers on the user and one such trigger is using enqueue method. The enqueued method is using a future method to update contact records and there are no enqueued jobs in any other trigger.

So my question as I am enqueuing only one class why I am getting this error when I am trying to update ures records?

This is the trigger

if(trigger.isAfter){
    if(trigger.isUpdate){
        System.enqueueJob(new GlobalPRM_UserTriggerHelper(Trigger.New, Trigger.oldMap));
    }
}
  • That error means one thing -- starting multiple child jobs from the same queueable job are not possible – kurunve Aug 8 '17 at 6:22
  • I need to find out where it is starting multiple child jobs as none of this code is built by me and I need to point out the exact problem as the client won't approve updating existing code without proper explanation. – Ishan Aug 8 '17 at 6:29
  • 2
    It seems to be very hard challenge for everyone that does not see full source code of the environment. I can recommend you to pull all metadata and check where queueable jobs are created and enqueued. One of that spots will be causing this issue – kurunve Aug 8 '17 at 6:44
  • 1
    It's likely you are firing the update trigger twice when you think you are only firing it once. If you are going to plug anything async into triggers you need to very carefully build a framework to support it. Not a trivial architecture. You should buy Advanced Apex and read the whole relevant chapter. – Adrian Larson Aug 8 '17 at 11:08
  • I was able to find a workflow which was running after every update on User record that was causing the problem. I Deactivated it and my code was running like Gem. I would be suggesting client to use System.isBatch() in the trigger. – Ishan Aug 8 '17 at 11:36
2

See: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

  1. Executes all after triggers
  2. ...
  3. ...
  4. Executes workflow rules.
  5. If there are workflow field updates, updates the record again.
  6. If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules, duplicate rules, and escalation rules are not run again.

It means that the trigger, that calls System.enqueueJob, can be run twice in one transaction.

  • You could also just have more than 200 records. – Adrian Larson Nov 16 '17 at 14:38

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.