Asynchronous Apex Interview Questions
Asynchronous Apex Interview Questions
Only in batch class finish method, We can call another batch class. If you will call
another batch class from batch class execute and start method, then Salesforce will
throw below runtime error.
Yes, it is possible. We can call a batch apex from trigger but we should always keep
in mind that we should not call batch apex from trigger each time as this will exceeds
the governor limit this is because of the reason that we can only have 5 apex jobs
queued or executing at a time.
8. Is Future method support primitive data types? Why sobject parameters not
supported?
The reason why sObjects can’t be passed as arguments to future methods is that the
sObject might change between the time you call the method and the time it
executes. In this case, the future method will get the old sObject values and might
overwrite them.
Yes.
Can’t call future from batch and future contexts, 1 call from queueable context is
allowed.
NOTE :-
1) Methods with the future annotation must be static methods
3) The specified parameters must be primitive data types, arrays of primitive data
types, or collections of primitive data types
4) Methods with the future annotation cannot take sObjects or objects as arguments.
5) You can invoke future methods the same way you invoke any other method.
However, a future method can’t invoke another future method
9) To test methods defined with the future annotation, call the class containing the
method in a startTest(), stopTest() code block. All asynchronous calls made after the
startTest method are collected by the system. When stopTest is executed, all
asynchronous processes are run synchronousl
Queueable Apex is similar to future methods in that they’re both queued for
execution, but they provide us these additional benefits.
When you queue a Queueable Apex, you get a job ID, that can be used to trace it
easily, which is not possible in case of future methods.
You can use non-primitive datatypes in Queueable Apex, like objects and sObjects,
which is not possible in case of future methods, because it supports only primitive
data types as params.
You can chain jobs, by calling another starting a second job from a running job,
which is not possible in case of future methods, because we can’t call another future
method from a future context.
Create a class, implement the Queueable interface, and override the execute
method.
19.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”
20.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.
The approach is we need to first check how many queueable jobs are added in the
queue in the current transaction by making use of Limits class. If the number has
reached the limit, then call a schedulable class and enqueue the queueable class
from the execute method of a schedulable class.
Asynchronous Apex
Apex offers multiple ways for running your Apex code asynchronously. Choose the
asynchronous Apex feature that best suits your needs.
This table lists the asynchronous Apex features and when to use each.
Batch Apex • For long-running jobs with large data volumes that
need to be performed in batches, such as database
maintenance jobs
• For jobs that need larger query results than regular
transactions allow
Asynchronous Apex When to Use
Feature
• Queueable Apex
Take control of your asynchronous Apex processes by using
the Queueable interface. This interface enables you to add jobs to the queue and
monitor them. Using the interface is an enhanced way of running your
asynchronous Apex code compared to using future methods.
• Apex Scheduler
• Batch Apex
• Future Methods
There are 4 Asynchronous Apex features their name and use cases are mentioned
below:-
1. Ensure fast execution of the of the future methods by tuning the query used in the
method, the longer the future method takes to complete the execution the more delay
occurs for the requests in the queue.
2. Testing the future method to the maximum numbers of the result expected to handle,
this will help in determining the delay
3. Use batch apex if the number future method invocation is reasonably large. Avoid
using a large number of future methods.
Queueable Apex is also a way of running Apex jobs asynchronously and to make a
class Queueable we need to implement an interface i.e. Queueable interface. With
the help of this interface, we can queue jobs and also monitor them, this is an
enhanced way of running the asynchronous process as compared to the future
method.
Queueable jobs are similar to the future method as both are queued before the
execution and get executed only after the resources are available, but the extra
feature it offers is below:
1. An id is generated when the job is queued, with help of this Id one can monitor the
progress of the job, from Salesforce UI or programmatically.
2. Non-primitive types are also allowed in these jobs, they can be accessed when the
job is executed.
3. Chaining of the job is possible in Queueable apex, but it can call only chain one
Queueable job
1. From system UI
2. Using System.Schedule method
The difference between the two ways is that in the system.schedule method we can
also specify the minutes and seconds as well but using the UI we can just mention
the hour of the day.
Schematics of how to schedule a class from UI
Step 1:
Step 2: To schedule weekly
To Schedule Monthly
Expression Description
1.
We can have only 100 apex scheduled jobs at one time in a particular
Organization which can be checked and counted at scheduled jobs page in
sSalesforce
1. They also count against the shared limit for the Asynchronous Apex i.e. 250000 for a
rolling 24-hours period.
2. Extreme care has to be taken if the job is getting scheduled via Apex Trigger
3. Synchronous Web service callouts are not supported by the schedule Apex, to make
call out we have to make use of the Asynchronous call out, however, a batch can job
can be scheduled and callouts are supported from the batch
4. If a scheduled job is supposed to run during the maintenance downtime will be
scheduled to execute after the maintenance work is completed.