Home Salesforce Revisit Asynchronous Apex : Type and Usage

Revisit Asynchronous Apex : Type and Usage

by Dhanik Lal Sahni

Asynchronous Apex
Asynchronous Apex is used to run processes in a separate thread, at a later time.

An asynchronous process is a process or function that executes a task in the background without the user having to wait for the task to finish.
In real work example, let us we want to send Adobe E-Sign document to customer when any application is created. Adobe esign normally takes 5-15 seconds to generate agreement. we can not hold customer till agreement is generated. So we can use some Asynchronous processing so that agreement generation done in background. Email will automatically sent to applicant for signature.

Benefit of Asynchronous Apex :
User efficiency
It is always better to give high performing application to user. If user will keep waiting for output they will get annoyed. So it is better, we can get task registered and then we can do action in background service. This way user experience will be better.
Yes, this will be dependent on type of action need to be done.

We can take action based on availability of required resources using platform events.

Higher Governor Limits
Asynchronous processes are executed in a new thread, with higher governor and execution limits.

Different Types of Asynchronous Apex

Future Methods:
Future Methods, Run in their own thread. These methods only start when resources are available. This is mainly used when web service callout is required.

global class FutureClass
    public static void myFutureMethod()
         // Perform some operations

Batch Apex:
These are used when we run large jobs that would exceed normal processing limits.Using Batch Apex, you can process records asynchronously in batches to stay within platform limits

global class MyBatchClass implements Database.Batchable {
    global (Database.QueryLocator | Iterable) start(Database.BatchableContext bc) {
        // collect the batches of records or objects to be passed to execute
    global void execute(Database.BatchableContext bc, List

records){ // process each batch of records } global void finish(Database.BatchableContext bc){ // execute any post-processing operations } }

Queueable Apex:
This is extension of future methods. We can chaining in queueable jobs. You can chain one job to another by starting a second job from a running job. Chaining jobs is useful if you need to do some processing that depends on another process to have run first. In chaining we get output as ID, which can be use to manage that job. Example for this is

public class AsyncExecutionController implements Queueable {
     public void execute(QueueableContext context) {
          Account a = new Account(Name='Dhanik',Phone='(0123) 555-1212');
          insert a; 

//jobid can be used for chaning or managing it.
ID jobID = System.enqueueJob(new AsyncExecutionController ());

Scheduled Apex:

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.

global class SomeClass implements Schedulable {
    global void execute(SchedulableContext ctx) {
        // awesome code here

Asynchronous processing has lower priority than real time interaction via the browser and API. To ensure there are sufficient resources to handle an increase in computing resources, the queuing framework monitors system resources such as server memory and CPU usage and reduce asynchronous processing when thresholds are exceeded.

You may also like

1 comment

Avoid Batch Apex and Use Queueable Class | SalesforceCodex November 5, 2021 - 9:55 pm

[…] Revisit Asynchronous Apex : Type and Usage […]


Leave a Comment