Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Que1.What is asynchronous 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.

We have multiple ways to implement Async Apex and these are :


(i). Future Method
(ii). Batch Apex
(iii).Queueable Apex
(iv). Schedulable Apex

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.

Que2.What is the Future Method ?


Ans : Future method is Asynchronous apex which runs in the background and runs the process
in separate thread when the system resource are available.

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.

global class myClass {


@future
public static void myFutureMethod(){
//perform some operation
}
}
Important points while writing a future method.
- A future method must be static method. It only returns void type
- It can’t take object and sObject as parameter
- Parameter must be primitive data type
- Future method cant be call from the trigger.
Que3.Why we can’t use sObject as an parameter ?
Ans : Because the object can change between the time you call the method and the time that it
actually execute.

Que4.Batch Apex Skeleton -


Ans :
public class LeadBatch implements Database.Batchable<SObject> {

/**
* 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');
}

You might also like