Thursday 24 August 2017

Synchronous and Asynchronous Apex

Synchronous Apex:
Synchronous execution takes place as a single transaction and it does not wait for available resources and in Apex code is executed in one single go.

Example:

Trigger
Controller Extension
Custom Controller


Asynchronous Apex:

Asynchronous apex is executed when resources are available. So any calling method which calls Asynchronous apex won't wait for the outcome of Asynchronous call.






1.Future Method


2.Batch Class

3.Schedule Class   

Future Method: 
future method runs in the background, asynchronously. You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you'd like to run in its own thread, on its own time. ... Each future method is queued and executes when system resources become available.



Ex1: 


global class FutureClass
{
    @future
    public static void myFutureMethod()
    {   
         // Perform some operations
    }
}

Ex2: 
global class FutureMethodRecordProcessing
{
    @future
    public static void processRecords(List<ID> recordIds)
    {   
         // Get those records based on the IDs
         List<Account> accts = [SELECT Name FROM Account WHERE Id IN :recordIds];
         // Process records
    }
}

Batch Class: 
Batch Clas is long-running processes that run on thousands of records on the Force.com platform.Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks.
To use batch Apex, write an Apex class that implements the Salesforce-provided interface Database.Batchable and then invoke the class programmatically.

Batchable Interface: Batchable interface can be execute as a batch apex job. 


Batchable Methods:

1.Start
2.Execute
3.Finish

Start: Start Method have prepared the record and return the set of records that will be batched for execution.start method execute one one time.

Syentex:
global Database.QueryLocator start(Database.BatchableContext bc) {}

Database.QueryLocater(Object) :  That object contains the records and return to the batch job.

Represents the record set returned by Database.getQueryLocator and used with Batch Apex.

When you’re using a simple query (SELECT) to generate the scope of objects in the batch job, use the Database.QueryLocator object. If you use a QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. 

For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an org. 

Execute: It will take the records prepared in start method and split those records into batches and it will execute multiple times. 

For example if the start method is returning 1000 records then execute method executes 5 times if you don't mention the batch size (Default Batch Size is: 200). Maximum batch size is: 2000.

Finish: Finish method is used to send email and any commit message and it's run only one time.

Batch class :
global class batchAccountUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {         String query = 'SELECT Id,Name FROM Account';         return Database.getQueryLocator(query);     }         global void execute(Database.BatchableContext BC, List<Account> scope) {          for(Account a : scope)          {              a.Name = a.Name + 'Account';                      }          update scope;     }             global void finish(Database.BatchableContext BC) {     } }




No comments:

Post a Comment

One particular lead takes how many days to change it's stage from one stage value to another

/*************** Ceated By    : Mohit Dwivedi( KVP Business Solution Pvt Ltd) . Created Date :  Purpose      : This  controller is for Lead ...