[go: up one dir, main page]

0% found this document useful (0 votes)
2K views21 pages

Asynchronous Apex Interview Questions

1. There are four main asynchronous Apex features in Salesforce: future methods, batch Apex, scheduled Apex, and queueable Apex. Each has different use cases for when to use them. 2. Future methods are best for preventing transaction delays from long-running processes or callouts. Batch Apex is suited for bulk data processing jobs. Scheduled Apex allows scheduling class invocations. Queueable Apex supports job chaining and passing complex objects between jobs. 3. Asynchronous Apex offers higher governor limits compared to synchronous code, and future methods run on a separate thread to avoid impacting the current transaction.

Uploaded by

Ankit Balapure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views21 pages

Asynchronous Apex Interview Questions

1. There are four main asynchronous Apex features in Salesforce: future methods, batch Apex, scheduled Apex, and queueable Apex. Each has different use cases for when to use them. 2. Future methods are best for preventing transaction delays from long-running processes or callouts. Batch Apex is suited for bulk data processing jobs. Scheduled Apex allows scheduling class invocations. Queueable Apex supports job chaining and passing complex objects between jobs. 3. Asynchronous Apex offers higher governor limits compared to synchronous code, and future methods run on a separate thread to avoid impacting the current transaction.

Uploaded by

Ankit Balapure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Asynchronous Apex Interview Questions

1.Can we call the batch in to another batch apex?

Yes, we can call from finish method.

2.Can we call batch apex in to another batch in excute method?

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.

System.AsyncException: Database.executeBatch cannot be called from a batch


start, batch execute, or future method.

3.Can we call the batch apex from triggers in salesforce?

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.

4.Can we call webservice callout from batch apex?

To make a Webservice callout in batch Apex, we have to


implement Database.AllowsCallouts interface.

5.How many times start,execute,finish methods will excute in batch apex?

Start method,finish method one time, execute method it depends on requirement.


Based on the batch size and data retrieved in Start method.

6.What is the Batch executions limit per day?

The maximum number of batch executions is 250,000 per 24 hours.

7.Can we call the future method in batch class?

No,we can’t call.

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.

9.How can I perform Callouts from Future methods?


We need to add a parameter callout=true in @future.

10.Can I write a future call in Trigger?

Yes.

11.What are the limitations of future methods?

It is not a good option to process large numbers of records.

Only primitive data types supported.

Tracing a future job is also typical.

Can’t call future from batch and future contexts, 1 call from queueable context is
allowed.

12.What is future method?

Use of future methods to isolate DML operations on different sObject types to


prevent the mixed DML error. Each future method is queued and executes when
system resources become available. That way, the execution of your code doesn’t
have to wait for the completion of a long-running operation. A benefit of using future
methods is that some governor limits are higher, such as SOQL query limits and
heap size limits

NOTE :-
1) Methods with the future annotation must be static methods

2) can only return a void type

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

6) No more than 50 method calls per Apex invocation

7) Asynchronous calls, such as @future or executeBatch, called in a startTest,


stopTest block, do not count against your limits for the number of queued jobs

8) The maximum number of future method invocations per a 24-hour period is


250,000 or the number of user licenses in your organization multiplied by 200,
whichever is greater

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

13.How does Queueable Apex differ from Future methods?

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.

14.Can you write a sample 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
}
}

15.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];

16.Can I do callouts from a Queueable Job?

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


from Queueable Jobs.

17.How many numbers of jobs, I can queue using System.enqueueJob() at a


time?

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


transaction in Synchronous apex. In asynchronous transactions, you can add only
one job to the queue.

18.Can I call Queueable from a batch?


Yes, But you’re limited to just one System.enqueueJob call per execute in the
Database.Batchable class. Salesforce has imposed this limitation to prevent
explosive execution.

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.

Asynchronous Apex When to Use


Feature

• To start a long-running operation and get an ID for it


Queueable Apex • To pass complex types to a job
• To chain jobs

Scheduled Apex • To schedule an Apex class to run on a specific


schedule

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

Future Methods • When you have a long-running method and need to


prevent delaying an Apex transaction
• When you make callouts to external Web services
• To segregate DML operations and bypass the mixed
save DML error

• 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. Future Methods: it is a basic asynchronous feature, we imply this method


when we want to prevent any delay in the transaction or when we make a web
call out or when we want to prevent the mixed DML error
2. Batch Apex: To do bulk processing of records or for the jobs that require
larger query result for example processes like Database maintenance jobs.
3. Schedule Apex: This feature is used to schedule the invocation of an apex
class at a specific time, this can be a recurring event or a one-time task.
4. Queueable Apex: When one task is dependent on completion of another task
we make use of this asynchronous feature, Also job chaining and complex
type of jobs are achieved using this feature

Asynchronous Apex: Future


Method
It is a method which runs on a separate thread in the background. It does not impact
the performance of a running transaction as it carried out in a different context. We
use this method for the execution of long running operation such as a web call out
where we are not sure when the response will come or whether a response will
come or not.
This is not the only use case of the future method we also use them to isolate DML
statement because there is a possibility that when two DML operation is performed
over different sObject then we might get a mixed DML operation error. Future
methods help us in preventing this error.

Image: Depicting the difference in thread processing between apex normal


transaction and in future method
The specialty of the Future method is that it executes when the resources are
available and does not out overhead on the server. This is the reason that execution
of the code does not have to wait for the completion of a long running operation and
also we get higher Governor limits when we use Asynchronous Apex.
Simple syntax of the future method is mentioned below:
Global class ClassNameOfFutureMethod
{
@Future
Public static void FuturemethoName()
{
ß Code to be executed à
}
}
Some consideration before we start implementing the future method is that it should
be declared as a static method and the only return type it can have is void. Secondly,
the parameter defined for the future method must be primitive i.e. no salesforce
sObject can be passed as a parameter to the future method. The reason an sObject
is not passed in the future method is that there is a possibility that the sent sObject
might change between the time the future method is executed.
To interact future method with the sObject that already exists in the database, we
can pass the ID of the object instead and then fetch the latest version of the record in
the method itself and then perform the task, as shown below.
global class ClassNameOfFutureMethod
{
@future
public static void FuturemethoName (List<ID> rIds)
{
// We can get the records based on the list of Ids
List<custObj> obj = [SELECT Name FROM custObj WHERE Id IN : rIds];
// We can Process records after fetching the records
}
}
Below is a skeletal code of a future method that interacts with an external web
service. For this we have to add an extra annotation to the method to make the
platform aware that this method will interact with an external service, as shown
below
global class ClassNameOfFutureMethod
{
@future(callout=true)
public static void FuturemethoName (List<ID> rIds)
{
// We can get the records based on the list of Ids
List<custObj> obj = [SELECT Name FROM custObj WHERE Id IN : rIds];
// We can Process records after fetching the records
}
}
Future methods are invoked as any other method in the apex, but a Future method
cannot invoke another future method. There are certain limitations of the future
methods in addition to the above-mentioned limitation which are given below:
No more than 50 methods with Future annotation can be called in a single apex
invocation.
Maximum 250000 invocation is allowed per rolling 24-hours or number of licenses
multiplied by 200 whichever is the greatest is the limit of the invocation. This limit is
for the entire organization and is shared with the entire Asynchronous apex, i.e.
Batch Apex, Queueable Apex and scheduled Apex.

Testing Future Methods


Since Future method is called in different context then the running thread, it has to
be tested in a different way than the normal. To make them run in the same context
we make use of startTest() and stopTest() code block. All asynchronous method
written after the startTest are collected by the system and are executed
synchronously after the stopTest method is called, as shown below.
@isTestprivate class ClassNameOfFutureMethodTest { @isTest static void test1()
{ Test.startTest(); ClassNameOfFutureMethod.FuturemethoName
(); Test.stopTest(); } // Verify the result with help of
system.assertEquals }}

Best practice and performance for


Future method
Salesforce implements a queue based framework for the handling of the
asynchronous processes, the queue is implied to balance the load of request across
organizations, following are the best practice for the future methods:

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.

Next, we will discuss the Queueable Apex


Asynchronous Apex:
Queueable Apex

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

Queueable jobs implements the Queueable interface, in this interface we have an


execute method which has return type of void and take QueueableContext as a
parameter. Below is an example of implementation of the Apex Queueable job:
public class QueueableClass implements Queueable {
public void execute(QueueableContext context) {
contact con = new contact(lastName=’Datta’,Phone=’9999999999′);
insert con;
}
}
In order to add this class to the queue list, we need to make use of
SystemEnqueueJob, these methods return an Id. With help of this Id, we can monitor
its progress and check its status.
ID queuejobID = System.enqueueJob(new QueueableClass());
AsyncApexJob queuejobInfo = [SELECT Status,NumberOfErrors FROM
AsyncApexJob WHERE Id=: queuejobID];

Testing Queueable Jobs


As we know that Queueable is an asynchronous job, therefore to test these classes
we have to make use of the startTest and StopTest methods, where all the
asynchronous method runs synchronously. Below is an example of the Queueable
job and the way to test it.
@isTest
public class QueueableClass Test {
static testmethod void test1() {
Test.startTest();
System.enqueueJob(new AsyncExecutionExample());
Test.stopTest();
Contact con = [SELECT lastName,Phone FROM Contact WHERE lastName =’Datta’
LIMIT 1];
System.assertNotEquals(null, con);
System.assertEquals(‘9999999999’, con.Phone);
}
}

Chaining the Jobs


There is a possibility that we want to perform a certain task after the completion of
another job. As discussed before, we can call another Queueable Job from inside of
a running queueable job. To do this we need to add another job to the queue in the
execute method of the presently running job. We achieve this as shown below.
public class QueueableClass implements Queueable {
public void execute(QueueableContext context) {
contact con = new contact(lastName=’Datta’,Phone=’9999999999′);
insert con;
System.enqueueJob(new anotherQueueableJobClass());
}
}

Limitations of Queueable Jobs


1. 50 jobs can be added to the queue with System.enqueueJob() method in a single
transaction
2. 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
3. Only one job can be chained at a time, i.e. only one child job from the same
Queueable job.

Now we will discuss Apex Scheduler

Asynchronous Apex: Apex


Scheduler
To make a class invocable at a specific time, first, we need to implement
Schedulable interface in the class and then schedule it either from Salesforce UI or
from the system.schedule method.

Implementing the Interface


schedulable
To invoke a class on a regular basis we first need to implement the Schedulable
interface, which like Queueable apex has an execute method. The scheduler run as
system and user permission has no significance in executing the class.
As mentioned above, while implementing schedulable interface we need to
implement the execute method of the interface which is a global method and takes
schedulableContext as an argument, its syntax is given below.
global void execute(SchedulableContext sc){}
This method is used to instantiate the class we want to schedule, as shown below.
global class MyScheduleClass implements Schedulable {
global void execute(SchedulableContext SC) {
Class2beSchedule M = new Class2beSchedule ();
}
}
The next method is used to schedule the class it is called system.schedule, as
shown below.
MyScheduleClass m = new MyScheduleClass ();
String CronExpression = ’20 30 8 10 2 ?’;
String SchedulejobID = system.schedule(‘My Job’, CronExpression, m);
The schedulable class can also be used to Schedule a batch class, as shown below.
global class MyScheduleClass implements Schedulable {
global void execute(SchedulableContext SC) {
batchclass M = new batchclass ();
database.executeBatch(M);
}
}
Batch can also be schedule using the method system.schedulebatch, about this we
will study in detail in later section.
Once the job is scheduled we can keep track of it with the help of
schedulableContext, with help of its getTriggerID we can retrieve the Id of the
scheduled job and we can query the CronTrigger Object and monitor the progress of
the job.
To schedule a class we discussed that there are two ways:

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

Let’s discuss how to create a cron expression:


Basic syntax of cron expression is
S M H DOM Mon DOW Yr
S- Seconds
M- Minutes
H- Hour
DOM- Day_of_month
Mon- month
DOW-Day_of_week
Yr -Year
Then there are some special characters which are used in conjunction with the
values like ‘*’, ‘?’, “,”,’-‘
Some sample cron Expressions are mentioned below:

Expression Description

0 0 13 * * ? Class runs every day at 1 PM.

0 0 22 ? * 6L Class runs the last Friday of every month at 10 PM.

0 0 10 ? * MON-FRI Class runs Monday through Friday at 10 AM.

0 0 20 * * ? 2010 Class runs every day at 8 PM during the year 2010.

Limitation of Apex Scheduler


Following are the limitations of Apex Scheduler:

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.

Now let’s shift our focus to batch class.

Asynchronous Apex: Batch


Class
Salesforce Governor limits us to build a long running process like data cleansing or
data archiving, where records more than 50000 are being processed at one time. To
fulfill this requirement Salesforce has to introduce the batch Apex, a batch job makes
smaller chunks of the data which is known as batch size and then asynchronously
work on each batch in a separate thread and therefore not hitting the governor limit.

ImageSource: https://i.stack.imgur.com/7mHOq.png How batch works


A batch job can be invoked at runtime programmatically using apex and it can also
be scheduled using a scheduled job as discussed above.

Using batch Apex


A Class to be executed as a Batch has to implement the batchable interface. In this
interface, we have to implement three methods, i.e Start, execute and finish
methods.

Image source: http://4.bp.blogspot.com/-


Cug5KaAx5Q4/UkjTViCbSnI/AAAAAAAAAFI/r4ayS5iarrw/s1600/marketing-process-
distribute1%5B2%5D.gif Schematic of a Batch Class
To collect the records on which we have to perform the bulk processing tasks are
collected in this method, this method returns an iterable or a Database.querylocator,
both of the return types have their use cases but mostly Database.querylocator is
used.
When a simple query is used to generate the collections of record, we should use
database.querylocator. This object by passes the Salesforce governor limit of 50000
records and can return up to 50 million records in an org.
Iterable is created only when we have to create a complex scope or collection of
records, Iterable is limited to 50000 records only.
The syntax of the start method is as below:
global (Database.QueryLocator | Iterable<sObject>)
start(Database.BatchableContext bc ) {}
After the collections of record is gathered the execute method is then next executed.
This method is called for each batch, it takes a reference to
database.BatchableContext object and a list of sObject. Batches are normally
executed in the order they are received, but their execution order is not guaranteed.
The syntax of Execute method is as below:
global void execute(Database.BatchableContext BC, list<P>){}
Once all the batches are executed, then the last method Finish is called. This
method takes the batchable context as a parameter and is used to perform post
processing task like sending an email.
Syntax of Finish method is as below:
global void finish(Database.BatchableContext BC){}
Each batch is considered as a separate transaction and they avail fresh set of apex
governor limit, i.e. if a batch of 500 records are being processed with scope of 50
then 10 batches are going to be executed separately and the governor limits will
reset for each batch.

Examples of batch Classes


global class UpdateAccountFields implements Database.Batchable<sObject>{
global final String Query;
global final String Entity;
global final String Field;
global final String Value;
global UpdateAccountFields(String q, String e, String f, String v){
Query=q; Entity=e; Field=f;Value=v;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,
List<sObject> scope){
for(Sobject s : scope){s.put(Field,Value);
} update scope;
}
global void finish(Database.BatchableContext BC){
}
}
To call this batch class we need to us the following code:
String q = ‘SELECT Industry FROM Account LIMIT 10’;
String e = ‘Account’;
String f = ‘Industry’;
String v = ‘Consulting’;
Id batchInstanceId = Database.executeBatch(new UpdateAccountFields(q,e,f,v), 5);
The following code is to re assign the account owner to a new one.
global class OwnerReassignment implements Database.Batchable<sObject>{
String query;
String email;
Id toUserId;
Id fromUserId;
global Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Account> accns = new List<Account>();
for(sObject s : scope){Account a = (Account)s;
if(a.OwnerId==fromUserId){
a.OwnerId=toUserId;
accns.add(a);
}
}
update accns;
}
global void finish(Database.BatchableContext BC){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {email});
mail.setReplyTo(‘batch@acme.com’);
mail.setSenderDisplayName(‘Batch Processing’);
mail.setSubject(‘Batch Process Completed’);
mail.setPlainTextBody(‘Batch Process has completed’);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
To call the owners reassign batch class we need to use the following code:
OwnerReassignment A = new OwnerReassignment();
A.query = ‘SELECT Id, Name, Ownerid FROM Account ‘ +
‘WHERE ownerid=\” + u.id + ‘\”;
A.email=’test@acme.com’;
A.fromUserId = u;
A.toUserId = u2;
ID id = Database.executeBatch(A);
To Allow Callout in the batch class we need it to implement another interface
i.e. Database.AllowsCallouts
Each batch execution is a different transaction and one transaction is unaware about
the status and progress of the other transaction. The entire batch executed are
asynchronous process and do not have a state after they have finished executing.
To maintain the state and pass a value from one batch to another we need to specify
another interface while defining the class, i.e. Database.Stateful.
Below is the code to aggregate the total of amount of opportunity.
global class SummarizeAccountTotal implements
Database.Batchable<sObject>, Database.Stateful{
global final String Query;
global integer Summary;
global SummarizeAccountTotal(String q){Query=q;
Summary = 0;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(
Database.BatchableContext BC,
List<sObject> scope){
for(sObject s : scope){
Summary = Integer.valueOf(s.get(‘total__c’))+Summary;
}
}
global void finish(Database.BatchableContext BC){
}
}

Batch Apex governor Limits


1. 5 batch jobs can run concurrently at a time in a given organization
2. Upto 100 jobs can be held in the Apex Flex queue.
3. Maximum number of batch that can be executed is 250000 per rolling 24 hour
periods. This limit is shared among the all asynchronous process like Future,
Queueable and schedulable.
4. Maximum of 50 mil records can be returned by Database.querylocator and if more
records are returned then the job is terminated immediately.
5. Maximum limit of a batch size is 2000, if higher value is set then the records are
chunked into size of 2000, in case of iterable there is no upper limit.
6. A batch can implement a maximum of 100 callout and the call can be from any
method i.e. start, execute and finish

Batch Apex Best Practice


1. Take extreme precaution when scheduling a batch from a Trigger, as it can breach
platform limit very easily.
2. While testing only one batch of apex can be tested using test.starttest() and
Test.StopTest methods().
3. Use Database.Stateful only if you want to share instance member variable value
across the whole job, otherwise it will impact the efficiency of the overall job
performance.
4. Future methods are not allowed in a batch class, it cannot execute a future method
5. Another Batch job can be called from the finish method of the Batch class, it is called
chaining batch job.

You might also like