Interview Question on Queueable Apex

 

What is the advantage of Queueable Apex over future methods?

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.
  • 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

What is the interface used for Queueable Apex?

Queueable interface

What are methods used in Queueable Apex Class?

Execute method

When will we use future methods instead of Queueable?

You use future methods instead of queueable is when your functionality is sometimes executed synchronously, and sometimes asynchronously. It’s much easier to refactor a method in this manner than converting to a queueable class. This is handy when you discover that part of your existing code needs to be moved to async execution. You can simply create a similar future method that wraps your synchronous method.

How many jobs can you chain from executing a job?

You can add only one job from an executing job, which means that only one child job can exist for each parent job.

How many jobs can you queue in a single transaction?

You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.

How can I use this Job Id to trace the Job?

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

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

Can I do callouts from a Queueable Job?

Yes, you have to implement the Database.AllowsCallouts interface to do callouts from Queueable Jobs.

If I have written more than one System.enqueueJob call, what will happen?

System will throw LimitException stating “Too many queueable jobs added to the queue: N”

I have a use case to call more than one Queueable Jobs from a Batch apex, how can I achieve it?

Since we can’t call more than one Queueable Job from each execution Context, We can go for scheduling the Queueable Jobs.

How to test Queueable Apex?

To ensure that the queueable process runs within the test method, the job is submitted to the queue between the Test.startTest and Test.stopTest block. The system executes all asynchronous processes started in a test method synchronously after the Test.startTest statement. Next, the test method verifies the results of the queueable job by querying the account records that the job updated.

What are Limitations of Queueable Jobs?

50 jobs can be added to the queue with System.enqueueJob() method in a single transaction

Maximum depth of chain job is 5 i.e. 4 child jobs and initial parent job for Developer and Trail organizations but there is no limit in other editions

Only one job can be chained at a time, i.e. only one child job from the same Queueable job.

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