top of page

Batch Apex Example In Salesforce | Account & Contact | Tech SkillBirds Enterprise | Part 2

skillbirdsjc

Example 1 :

global class BatchApexExample implements Database.Batchable<sObject> {

global Database.QueryLocator start(Database.BatchableContext BC) {

// collect the batches of records or objects to be passed to execute

String query = 'SELECT Id, Name FROM Account';

return Database.getQueryLocator(query);

}

global void execute(Database.BatchableContext BC, List<Account> accList) {

// process each batch of records default size is 200

for(Account acc : accList) {

// Update the Account Name

acc.Name = acc.Name + 'sfdcpoint';

}

try {

// Update the Account Record

update accList;

} catch(Exception e) {

System.debug(e);

}

}

global void finish(Database.BatchableContext BC) {

// execute any post-processing operations like sending email

}

}


Example 2 :


public class UpdateContactFields implements Database.Batchable<sObject>{

public String query;

public Database.QueryLocator start(Database.BatchableContext BC){

return Database.getQueryLocator(query);

}

public void execute(Database.BatchableContext BC, List<sObject> scope){

List<Contact> contacts = new List<Contact>();

for(sObject s : scope)

{

Contact con = (Contact)s;

con.Description = 'Belongs to inactive account';

contacts.add(con);

}

update contacts;

}

public void finish(Database.BatchableContext BC)

{

}

}


Example 3 :


public class batchClass implements Database.batchable{

public Iterable start(Database.BatchableContext info){

return new CustomAccountIterable();

}

public void execute(Database.BatchableContext info, List<Account> scope){

List<Account> accsToUpdate = new List<Account>();

for(Account a : scope){

a.Name = 'true';

a.NumberOfEmployees = 70;

accsToUpdate.add(a);

}

update accsToUpdate;

}

public void finish(Database.BatchableContext info){

}

}


Example 4 :


public class MyBatchJob implements

Database.Batchable<sObject>, Database.Stateful{


public integer summary;

public MyBatchJob(String q){

Summary = 0;

}


public Database.QueryLocator start(Database.BatchableContext BC){

return Database.getQueryLocator(query);

}

public void execute(

Database.BatchableContext BC,

List<sObject> scope){

for(sObject s : scope){

Summary ++;

}

}


public void finish(Database.BatchableContext BC){

}

}


Example 5 :


global class AccountUpdateBatch implements Database.Batchable<SObject> {

global Database.QueryLocator start(Database.BatchableContext BC) {

// Query to fetch account records

String query = 'SELECT Id, Name FROM Account WHERE CreatedDate = TODAY';

return Database.getQueryLocator(query);

}


global void execute(Database.BatchableContext BC, List<Account> scope) {

// Process each batch of records

List<Account> accountUpdateList = new List<Account>();

for (Account acc : scope) {

acc.Description = 'Updated by system batch process';

accountUpdateList.add(acc);

}

if(accountUpdateList.size()>0){

update accountUpdateList;

}

}



 
 
 

Comments


bottom of page