Asynchronous Apex

Asynchronous Apex 

Asynchronous Apex has various benefits of running jobs asynchronously we can do it in different ways as per the requirement of our task. Programs are isolated from other apex processes. They run on their own separate thread

When the resources are free with little higher than standard Governer limits.

These classes will always run independently and would never block any other standard process nor it will hold any resources.

Apex provides four different ways of implementing Asynchronous classes 

  • Future methods 

Futur methods are used in scenarios where it is a long-running method and needs to avoid delays or when there is a requirement of callouts for web services to external servers. we define multiple methods as future methods when we want them to run separately. When it makes a callout to external servers for web services it does not wait for the response it keeps executing and later it gets a response from the server.

 

Syntax : 

Public class Testclass 

{

@future /we mention future annotations before method declarartion

   Public static  void futuremethod( )

{

sample Codes  here;

}

}

 

Limitations of Future methods:

-These methods cannot be batched 

-Returns type is always void cannot return any data type.

– Works with only primitive data types.

– Cannot pass the sObjects in the future methods

  • Queueable 

Queueable Classes also runs Asynchronously and similar to future methods but has some extra advantages over future methods and fills all the room for improvement in future methods:

  • like we can use non-Primitive data types in queueable.
  • we can monitor job status with job id / Lightning UI.
  • Chaining of queueable methods are possible 
  • Processes are Added in jobQueue which can be queued from UI as well.

We implement the Queueable interface in our apex class that has long running methods.

Syntax: 

Public class testClass Implements Queueable Database.AllowCallouts {

Public void execute(QueueableContext QC)

{

Sample Codes here

}

}

Queueable class cann be used for making callouts and web services.

  • Batchable Apex 

When we come across a requirement where we have to modify very very large number of records in our org we are prevented to do this by Governor Limits

Which allows only 10,000 records per translation. In this scenario we have Batch apex for our rescue it Takes all the job and divide it into batches of small 200 records each and execute those batch asynchronously in the org when the resources are idle.  Class that implements batchable interface  has three functions to implement inside it Start , execute and finish .

Start function has query and take the records from org and divide those into small chunks of 200 records each .

In the Execute function we do the processing. The number of times the execute method will run depends on the batch size and number of total records.

Finish is the final method executed once for all the post processing tasks like monitoring.

Syntax :

Public class sampleBatch implements Database.Batchable<sObject. {

Public Database.queryLocator start(Database.BatchableContext bc)

{ return Database.queryLocator ;

}

Public void execute( Database.BatchableContext bc)

 {  sample code};

Public void finish(Database.BatchableContext bc )

{Sample Codes };

}

  • Schedulable Method 

We can Implement Schedulable interface for our classes to make it run on the specific time or at specified intervals. 

We use instance of our schedulable class to run at at specific time using using system.schedule method.

Cron Expression helps us specifying the time for class to run .

Cron exp. , Batch name and Class instance is passed as parameters to system.schedule method

Syntax : 

Global Class sampleSched implements schedulable {

Global void execute(SchedulableContext sc)

{

Sample codes here 

}

}

To invoke the class , we can use 

String  jobId =system.schedule(‘New Batch’, cronexp , new sampleSched( ) );

Summary
Article Name
Asynchronous Apex
Description
Asynchronous Apex is a function or process which helps in executing the task in the background where users don’t have to worry about a particular task to finish first.
Author
Publisher Name
BugendaiTech Pvt Ltd
Publisher Logo
Spread the updates

Related Posts

Leave a reply