[SalesForce] Too many queueable jobs added to the queue: 2

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));
    }
}

Best Answer

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.

Related Topic