[SalesForce] How to capture Queueable class status when job is completed

I have created Queueable class to process data and making the callouts this Queueable class executes upon after update and after insert of Opportunity object.

Now in order to track the status of Queueable class I am inserting the status of job into another object i.e. LogInfo object. The purpose of this object is to keep track of jobs, status and error if any.

When the queueable gets executed i do insert the status of it using following code

Execute method of Queueable class

 public void execute(QueueableContext context) {


     SomeHandler.insertStatus(context.getJobId());

 }

 class SomeHandler {

    public Id insertStatus(Id jobId){


     AsyncApexJob apexJob = [SELECT Id ,Status,CompletedDate,ExtendedStatus, 
     NumberOfErrors,JobItemsProcessed,TotalJobItems,CreatedBy.Email 
                                FROM AsyncApexJob 
                                WHERE Id = :jobId];

     LogInfo__c logInfoObj = new LogInfo__c();
     // add job id 
     logInfoObj.JobId__c = jobId;
     // update status
     logInfoObj.JobStatus__c = apexJob.Status;



    }

 }

SomeHandler.insertStatus is being used by multiple Queueable classes , now the issue is so far I am able to record only Processing status as all calls to insertStatus are being made within the exectue method and what I really want to record is the Completed status. However, Queueable class does not have the finish method we do not have any space or hook where in we can record the Queueable class Completed status. In the end we waned to display the real-time dashboard of how many jobs are in the queue and how many are being processed , failed, and completed.

Solutions did not work so far

  1. Can not create a trigger on AsyncApexJob object
  2. Can not use Transaction Finalizers as it is a pilot and available for specially-configured scratch orgs

Best Answer

It looks like you are trying to create a apex jobs view which is available out of the box with with new apex jobs interface.

If you want to do this manually there are two options:

  1. Put you queue able code in try/catch and update the job status to complete at the end of the queuable as last line in try block or as a failure in the catch block . It may not work if the errors are non catch-able like limit exceptions.
  2. Write a batch which runs every 15 minutes, queries the job status for records in log info and updates them.
Related Topic