FUTURE QUEUEABLE MONITORING
BATCH SCHEDULAR
Asynchronous Apex
Salesforce Apex Tutorial
@ soham-datta
What is Asynchronous Apex?
It is used to run processes in a separate
thread, at a later time.
It is a process or function that executes a task
"in the background" without the user having to
wait for the task to finish.
It typically uses for callouts to external
systems, operations that require higher limits,
and code that needs to run at a certain time.
The key benefits include:
User efficiency, Scalability, Higher Limits
Types of Asynchronous Apex?
It comes in four different flavors includes:
Future Methods
Batch Apex
Queueable Apex
Scheduled Apex
In upcoming slides we will discuss this in details.
Future Methods
Methods with the @future annotation must be
static methods, and can only return a void type
Typically used for:
Callouts to external Web services.
Operations need to run in their own thread
Isolate DML operations to prevent the
mixed DML error.
Future methods can’t be used in Visualforce
controllers.
Objects can’t be passed as arguments to
future methods.
Future method sample code block
The system collects all asynchronous calls
made after the startTest().
When stopTest() is executed, all these
collected asynchronous processes are then
run synchronously.
Batch Apex
Batch Apex is used to run large jobs (think
thousands or millions of records!) that would
exceed normal processing limits.
This functionality has two awesome
advantages:
Every transaction starts with a new set of
governor limits.
If one batch fails to process successfully,
all other successful batch transactions
aren’t rolled back.
It contains three methods:
start(), execute(), and finish()
Batch apex sample code block
It implements following interfaces including:
Database.Batchable
Database.Stateful
Database.AllowsCallouts
Queueable Apex
It is essentially a superset of future methods
with some extra features.
Additional benefits over future method:
1. Queueable class can contain member
variables of non-primitive data types, such as
sObjects or custom Apex types.
2. It returns the ID of the AsyncApexJob record.
You can use this ID to identify your job and
monitor its progress.
3. You can chain one job to another job by
starting a second job from a running job.
Queueable apex sample code block
Best practices:
The execution of a queued job counts once
against the shared limit for asynchronous
Apex method executions.
You can add up to 50 jobs to the queue with
System.enqueueJob in a single transaction.
Apex Scheduler
The Apex Scheduler lets you delay execution
so that you can run Apex classes at a
specified time.
This is ideal for daily or weekly maintenance
tasks using Batch Apex.
Schedule class sample code block
Monitoring Asynchronous Jobs
You can monitor the status of all jobs in the
Salesforce user interface.
From Setup -> enter Jobs in the Quick Find box
-> select Apex Jobs.
Let's Connect
@ soham-datta