[0:00]Hey everyone, it's great to have you back on SFDC Ninja. In today's video, I'll be talking about interview questions related to Queable Apex.
[0:08]So without wasting any more time, let's get started. Let's start with a very basic question, why do we use Queable Apex?
[0:14]So guys, it is very obvious that Queble Apex is used to execute apex code asynchronously. It is particularly useful for long-running processes like external web service call outs or extensive database operations.
[0:27]Guys, see, it is similar to future method, but with some additional benefits. Basically, it is an extension of future method.
[0:36]We will understand this thing properly in next question. So let's move to second question, which is what is the difference between future and Queable?
[0:42]So future method has some limitations like future method supports only primitive data types. For example, we cannot pass S object as parameter in future method.
[0:51]But in Queable Apex, it supports both primitive and non-primitive data types and of course, we can pass S object as parameter in Queable.
[1:00]Second limitation, as future method do not return job ID, that's why we cannot monitor it directly.
[1:06]But in case of Queable, when you submit your job by invoking system.enqueue job method, then that method returns the ID of new job.
[1:14]And using this ID, we can monitor the Queble job. So, when you queue a Queble Apex, you get a job ID that can be used to monitor it easily, which is not possible in case of future methods.
[1:25]Third limitation in future method, chaining is not possible. I mean, we cannot call future method from another future method.
[1:32]But in Queable, you can chain one job to another job. Basically, chaining is useful if your process depends on another process to have ran first.
[1:41]For example, let's consider a scenario. Let's say you are working on an e-commerce app in which order placement triggers a sequence of automated tasks.
[1:49]Like inventory check, payment processing, order confirmation email, and do some other tasks like inventory update.
[1:57]So doing all these things synchronously is very risky. So instead of doing these all things synchronously, each of these tasks can be implemented as a separate Queble job.
[2:08]Next question is what is Queable context? So it is an interface provided by Salesforce, which is basically used to create a Queable class.
[2:16]And it contains the job ID, which can be used to monitor a Queable Apex. Next question is, what is the use of System.enqueueJob()?
[2:24]So we use this method to add Queable job to the asynchronous execution queue. Next is, can we do callouts from Queable Apex?
[2:34]So yes, we can do callouts on Queable class. For that, you have to implement an interface, which is database.allows callout in your class.
[2:41]Next is, can we do call out in chaining? So yes, callouts are also allowed in chain Queable jobs.
[2:48]Next question is, how many number of jobs can be chained at a time? So guys, there is no limit is enforced on the depth of chained jobs.
[2:56]You can chain one job to another and you can repeat this process with new child job to link it to a new child job.
[3:03]But for developer edition and trial orgs, the maximum stack depth for chained job is five, which means that you can chain jobs four times.
[3:12]And the maximum number of jobs in the chain is five, which includes the initial parent job also. Next question is, how many number of jobs can be queued using System.enqueueJob() method?
[3:22]So you can add 50 jobs to the queue with this method in a single transaction. While in asynchronous transaction, like from a batch Apex job, you can add only one job to the queue.
[3:34]And if you want to check how many Queble jobs have been added in one transaction, then there is a method which is limits.getQueble job. You can use it for that.
[3:43]Next question is, can we call Queable from Batch Apex? So yes, we can call Queable from batch, but you are limited to just one system.enqueue job call per executing batch class.
[3:54]Next question is, can we start multiple child jobs from same Queable job? So guys, see, starting multiple child jobs from the same parent job is not supported.
[4:05]Which means only one child job can exist for each parent. Let's move to next question.
[4:12]Which is, how can we call more than one Queable job from Batch Apex? Since we cannot call more than one Queable job from batch, therefore, we can go for scheduling the Queable jobs.
[4:23]In this approach, we will first check the current number of Queable job in the queue. For that, we can use the method, which is limits.getLimit Queable jobs.
[4:33]Then, if the number reaches the limit, we will create a schedule class and inside execute method of schedule class, we will enqueue your Queable job.
[4:42]So by this way, we can call more than one Queable class from batch. Next is, how to test Queable job?
[4:50]See, a Queable job is an asynchronous process. So to ensure that this process runs within the test method, the job must be submitted to the queue between test.start test and test.stop test method.
[5:03]So these all are the questions. If you have any other question related to Queble effects, you can put it in comment section.
[5:09]That's it for today guys and I'll be back with more interesting videos. Thank you and keep watching.



