Salesforce Apex Class

Learn About All the Different Interfaces in Salesforce Apex Class

Need for @future Methods  

@future need resources for a longer time to make an API callout, an external web service to perform bulk data operation (batch apex) 

Governer limits to avoid mixed DML operation.

execute:class name.method name  

database.batchable== interface 

start,execute,finish 

database.batchableContext== 

grtJobId,getChildJobId() 

dont miss out iconDon't forget to check out: Salesforce Apex Tutorial for beginners | Apex Salesforce Tutorial

SETUP OBJECTS: ex-userRole, if changes are made on u, u r going to affect the permissions on the database

NON-SETUP OBJECTS: ex-account, no impact on permissions within a single method, you cannot create setup and non-setup objects in a single transaction for knowing status  

list<asyncapexjob> jobs=[select id,status,Jobtype from AsyncApexJob]; 

50 future method queue can be called within a queue but a future cannot be called within a future.sequence with asynchronous process chaing of job available in the queue. 

Support sObjects gives record id, the id of the job  

Database.stateful interface 

In Batch apex Changes made by one execute do not transfer to the other execute, so we need to implement “Database.Stateful” if the batch process needs information that is shared across transactions. 

In other words, If you want to transfer the state of the data from one batch to another batch we need to implement “Database.Stateful” interface. 

If you specify Database.Stateful in the class definition, you can maintain state across these transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don’t retain their values and are reset between transactions. Maintaining state is useful for counting or summarizing records as they’re processed. For example, suppose your job processed opportunity records. You could define a method in execute to aggregate totals of the opportunity amounts as they were processed. 

If you don’t specify Database.Stateful, all static and instance member variables are set back to their original values. 

To use Batch Apex, we must implement Database.Batchable interface, below are the methods which we use: 

  1. Start 
  2. Execute 
  3. Finish 

dont miss out iconCheck out anther amazing blog by Navdita here: Multi-Factor Authentication in Salesforce - A Short Guide

Start:

This method is called at the beginning of the batch apex job. This method will return either an object or type Database.QueryLocator or an iterable that contains the records or objects passed to the job. This method will collect records on which the operation will be performed.  

These records are divided into batches and passed to execute method. 

Execute:

This method contains the set of instructions which we want to perform on those records which we retrieved from the Start method.        

Finish:

This method gets executed after all the batches are processed. We use this method to send emails to inform them about the job completion. 

Below is an example: 

global class AccountBatchUpdate 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 + ‘******’; 
       } 
       update scope; 
   }  
   global void finish(Database.BatchableContext BC) { } 
}

Responses

Popular Salesforce Blogs