Intermediate to Advance Level Interview Questions on Queueable Apex

 

Q. Can you write a blueprint of Queueable Job?

Create a class, implement the Queueable interface, and override the execute method.

public class QueueableApexExample implements Queueable {
    public void execute(QueueableContext context) {
        //some process     
    }
}

Q. What is the return type of the id of Queueable apex in test class? 

The ID of a queueable Apex job isn’t returned in test context—System.enqueueJob returns null in a running test

Q. What is QueueableContext?

It is an interface that is implemented internally by Apex, and contains the job ID. Once you queue the Queueable Job, the Job Id will be returned to you, by apex through QueueableContext’s getJobId() method.

Q. How can I queue Queueable Job?

Using System.enqueueJob Method.

ID jobID = System.enqueueJob(new QueueableApexExample());

Q. How can I use the Job Id to trace the Queuable Job?

Just perform a SOQL query on AsyncApexJob by filtering on the job ID.

AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];

Q. I have 200 records to be processed using Queueable Apex, How Can I divide the execution Context for every 100 records?

Similar to future jobs, queueable jobs don’t process batches, so you can’t divide the execution Context. It will process all 200 records, in a single execution Context

Q. How Chaining works in Queueable Apex?

public class A implements Queueable {
    public void execute(QueueableContext context) {
        // Chain this job to next job by submitting the next job
        System.enqueueJob(new B());   
    }
}public class B implements Queueable {
    public void execute(QueueableContext context) {
        // Chain this job to next job by submitting the next job
        System.enqueueJob(new C());   
    }
}public class C implements Queueable {
    public void execute(QueueableContext context) {
        // Chain this job to next job by submitting the next job
        System.enqueueJob(new D());   
    }
}

Q. Can I chain a job that has implemented allowsCalloutsfrom a Job that doesn’t have?

Yescallouts are also allowed in chained queueable jobs.

Q. How to test Chaining?

You can’t chain queueable jobs in an Apex test. So you have to write separate test cases for each chained queueable job. Also, while chaining the jobs, add a check of Test.isRunningTest() before calling the enqueueJob.

Test.isRunningTest() Returns true if the currently executing code was called by code contained in a test method, false otherwise

public class A implements Queueable {
    public void execute(QueueableContext context) {
// Test to make sure that Unit test are not running before chaining
// the call. We don’t want to chain another job during a Unit test run.

        if(!Test.isRunningTest()){
          System.enqueueJob(new B());
       }   
    }
}

Q. What is Transaction Finalizers (Beta) ?

Transaction Finalizers feature enables you to attach actions, using the System.Finalizer interface, to asynchronous Apex jobs that use the Queueable framework.

Before Transaction Finalizers, there was no direct way for you to specify actions to be taken when asynchronous jobs succeeded or failed. With transaction finalizers, you can attach a post-action sequence to a Queueable job and take relevant actions based on the job execution result.

A Queueable job that failed due to an unhandled exception can be successively re-enqueued five times by a Transaction.

Example: We try to make a callout to an external platform, because of network issue, if the callout fails, how do we make sure that the callout is made again and re-enqueued?

We need to get the JobId after the callout is made, then check the status of the Job, if it failed then we need to re-enqueue it manually.

Salesforce team launched something called Finalizers which will make the whole process of re-enqueueing the failed queueable job so easy.

Step 1 – Implement a class that implements the System.Finalizer interface.

Step 2 – Attach a finalizer within a queueable job’s execute method by invoking the System.attachFinalizer method with an argument of the instantiated class that implements the finalizer interface. Only one finalizer can be attached to any queueable job.

public class UdpateFinalizer implements Finalizer {
   
    public void execute(FinalizerContext ctx) {
	//FinalizerContext is a class that provides us access to 4 methods.	
        //getRequestId()
        //getAsyncApexJobId()
        //getResult()
        //getException()
        
        String reqId = ctx.getRequestId();
        String jobId = Id.valueOf(ctx.getAsyncApexJobId());
        
        if (ctx.getResult() == ParentJobResult.SUCCESS) {
            System.debug('Parent Queueable (job id: ' + jobId + '): completed successfully!');

        } else { // Queueable failed
            //provide a counter & it should not exceed 5 times
            //re-enqueue the job
        }
    }

}
public class SampleAsyncJob implements Queueable {
    
    public void execute(QueueableContext ctx) {
        
        UdpateFinalizer f = new UdpateFinalizer();
        System.attachFinalizer(f);
        
        //Business logic
    }
    
}

Q. Differences between Future and Queueable Apex?

Future Method Queueable Job
1. Future will never use to work on SObjects or object types.

2. When using the future method we cannot monitor the jobs which are in process.

3. The future method cannot be called inside the future or batch class.

4. The future method will never be queued.

1. Queueable Jobs can contain the member variable as SObjects or custom Apex Types.

2. When using queueable jobs it will make the AsyncApexJob which we can monitor like Scheduled jobs.

3. Queueable Apex can be called from the future and batch class.

4. Using Queueable Apex will chain up to queueable jobs and in Developer Edition it is only 5 Jobs.

 

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes