Professional Documents
Culture Documents
Async Apex
Async Apex
Ans : Asynchronous apex is a process that execute the task in the background without the user
having to wait for the task to finish.So In Asynchronous Apex code runs in a multiple thread and
have to do many task as a background Job.
Where,
- Future method is a Async Apex which runs in the background and runs the process in
separate thread when the system resource are available.
- Batch class is used when we have to process large no record like 50k-50 Millions of
record batch class helps us to remain is platform limit
Batch execute is different- different batch size where the min size of a batch is 1 and
max is 2000 and default size is 200
- Queueable Apex are similar to future method but unlike future method here you can call
one queueable class from another queueable a
- lso you can use sObject as a parameter.
- Using schedulable apex we can run an apex class at a specific time and for using this a
apex class must implement the schedulable interface.
We use @Future annotation to declare a Future method and benefit of future method it avoid
the user from blocking or performing the different task and providing it the higher
governor/execution limit in the process.
/**
* Start Method
* Executes only once
* Return batch scope or records
* Can return upto 50 million records
* */
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id, LeadSource FROM Lead');
}
/**
* Execute Method
* Executes for each batch/chunk of records
* Must have void return type
* Each execution will get new set of governor limits
* */
public void execute(Database.BatchableContext BC, List<Lead> scope){
for(Lead leadRecord : scope){
leadRecord.LeadSource = 'Web';
}
update scope;
}
/**
* Finish Method
* Executes only once
* Called after all batches are processed
* Can be used for post processing
* */
public void finish(Database.BatchableContext BC){
System.debug('Batch finished');
}