Apex Queuable – Check if job has already been queued

Released as part of Winter ’15. Queueable Apex allows you to submit jobs for asynchronous processing similar to future methods with the following additional benefits:

  • Non-primitive types: Your Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. Those objects can be accessed when the job executes.
  • Monitoring: When you submit your job by invoking the System.enqueueJob method, the method returns the ID of the AsyncApexJob record. You can use this ID to identify your job and monitor its progress, either through the Salesforce user interface in the Apex Jobs page, or programmatically by querying your record from AsyncApexJob.
  • Chaining jobs: You can chain one job to another job by starting a second job from a running job. Chaining jobs is useful if you need to do some sequential processing.

Although you can chain jobs, if a trigger happens to fire twice due to an update on a record after the trigger has already processed it. This can happen if you have not implemented any frameworks or checks to prevent this.

A quick solution to this issue is to check in your code if it has already executed by retrieving the number of queueable jobs for the transaction.

if(Limits.getQueueableJobs() == 0){

    // Create Queueable Job Here
}

Leave a Reply