11
FebPROMO : GET UP TO 20% OFF ON LIVE CLASSES + 2 SELF-PACED COURSES FREE!! - SCHEDULE CALL
Asynchronous Apex in Salesforce is a true Savior from the Salesforce governor limits. Everybody knows how complex are governor limits in Salesforce. When there are a limited number of resources available in a multi-tenant architecture, it is hard to decide how to utilize resources to their best limits. So, what should you do when you want to process a huge amount of data together? To avoid this issue or we can say to maximize the resources utilization, the role of asynchronous apex came into the picture.
Salesforce apex classes have introduced the concept of Salesforce asynchronous apex that provides us with a double of the governor limits for almost each software program. Also, it is possible to perform up to 50 million operations on records using asynchronous apex Salesforce. There are multiple Asynchronous apex features in Salesforce and you have to decide which Salesforce asynchronous callout suits your business goals the most. Let us start our discussion with asynchronous apex jobs and types of Asynchronous Apex Salesforce CRM.
In Salesforce Asynchronous apex, the thread will not wait until it completes its tasks before proceeding to next. Instead, it proceeds to next leave it, runs in a separate thread. In the Salesforce asynchronous callout, the code runs in multiple threads that help to do many tasks as background jobs offer multiple ways for running your Apex code asynchronously. Moving ahead, you have to choose the asynchronous Apex feature that best suits your needs.
Learn Salesforce in the Easiest Way
Moving ahead to Salesforce Asynchronous callout, let us discuss batch apex and future methods in details that are frequently used by Salesforce Developers.
It is the basic Asynchronous apex in Salesforce that is used to prevent any delay in a transaction during a web callout. A future method executes when it has resources available. A future method runs in the background, asynchronously. It is used for executing long-running operations like Web services, call outs or any other operation which has to run on its own time. If Future Annotation is not used in a web service call out, the thread will wait until the response comes and other processes will not be executed. Future methods are used to avoid the mixed DML errors. It is queued and executed when resources are available to. So, it doesn’t have to wait for the completion of a long-running operation. A future method increases the governor limits, such as SOQL query limits and heap size limits. It is defined by @future annotation.
Read: How to Integrate Salesforce with Outlook, Gmail, & Mailchimp
@Future:
Future annotation is used to execute methods asynchronously. When you define @Future, it executes when Salesforce has available resources.
@Future (callout = true)
Example
public class DemoFuture {
public void insertData(){
DemoFuture.ExampleMethod();
Account acc=new Account(Name='Testing Record',industry='Energy',type='Other',phone='+22-3535543');
insertacc;
system.debug(acc);
}
@future
public static void ExampleMethod (){
profile p=[select id,name FROM profile WHERE name='Standard Platform User'];
user u =newuser(alias='Gorge',email='[email protected]',
emailencodingkey='UTF-8',lastname='Samuel',languagelocalekey='en_US',
localesidkey='en_US',profileid=p.id, timezonesidkey='America/Los_Angeles',
userName='[email protected]',communityNickName='akash',isactive=false);
insert u;
system.debug(u);
}
}
A user inserted Record Insert
This Asynchronous Apex Salesforce feature is used to process multiple records for the job together that require a larger query result. Batch Apex is an asynchronous job. Batch apex allows a single job that can be broken into smaller parts, where every record can be processed separately.
It can process a large number of records with more flexible governor limits than the synchronous code. When a batch class invokes, the job is placed on the Apex job queue and is executed as a separate transaction. When to use Batch Apex?
Salesforce Training For Administrators & Developers
Read: How To Get A Salesforce Job Without Experience?
Syntax: Developers need to define Databases.Batchable Interface
Global class BatchExample implements Database.Batchable<sObject>{
}
These are the governor Limits you need to keep in mind when dealing with Batch Salesforce asynchronous apex
There are three methods in batch apex
This method will be called at the starting of the Batch Job and collects the data on which the Batch job will be operating. Note the following points:
Syntax
globalDatabase.QueryLocator start(Database.BatchableContext b){
returnDatabase.getQueryLocator('select id, name from account );
}
The record which returned from start methods is executed into small chunks. Now every batch is separate runs the execute method.
Global void Execute (Database.BatchableContextb,list<sObject>acc){
}
This method gets called at the end and you could do some finishing activities like sending an email, call another batch job. Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each. The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back. Syntax
Read: What Apex Email in Salesforce?
Global void finish(Database.BatchableContext b){
}
Example
global class batchExample implements Database.Batchable<sobject>{
globalDatabase.QueryLocator start(Database.BatchableContext b){
returnDatabase.getQueryLocator('select id, name from account where name=\'United\'');
}
Global void Execute (Database.BatchableContextb,list<account>acc){
user u=[select id from user where alias='peter'];
for(account a:acc){
if(a.industry=='Consulting'){
a.industry='Education';
a.OwnerId=u.id;
}
}
updateacc;
}
Global void finish(Database.BatchableContext b){
messaging.SingleEmailMessage email1= new messaging.SingleEmailMessage();
string [] toAdd= new string[]{'[email protected]'};
string [] toBcc= new string[]{'[email protected]'};
email1.setToAddresses(toadd);
// email1.setToAddresses(toBcc);
email1.SetSubject('work is completed');
email1.SetSenderDisplayName('JnabaskTraing');
email1.setPlainTextBody('batch apex process is completed');
messaging.Email[]email= new messaging.Email[]{email1};
messaging.sendEmail(email);
}
}
batchExample SS = new batchExample();
Database.executeBatch(SS, 5);
system.debug(ss);
Email response
Salesforce Training For Administrators & Developers
This blog gives you a clear idea of why to use an asynchronous apex in Salesforce and how it can be beneficial for your business. The major benefits we noticed are more flexible governor limits in Salesforce asynchronous apex when compared to the synchronous apex code. The concept may be a little tough to understand at first glance but you will practice it quickly with continuous effort and dedication. All the Best!
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
AWS
DevOps
Data Science
Hadoop
Salesforce
QA
Business Analyst
MS SQL Server
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
What Is Trigger In Salesforce?
576.8k
Salesforce Admin Salary - How Much Does a Salesforce Admin Make?
7.8k
What is the Collection in Salesforce? (2020 updated)
648.7k
Learn Salesforce from Scratch with Salesforce Beginner Tutorial
553k
What is the Salesforce AppExchange? Packages Installation Guide
230.3k
Receive Latest Materials and Offers on Salesforce Course
Interviews