Syntax
=======
global class MyBatchClass implements Database.Batchable<sObject> {
global (Database.QueryLocator | Iterable<sObject>)
start(Database.BatchableContextbc) {
// collect the batches of records or objects to be passed to execute
}
global void execute(Database.BatchableContextbc, List<P> records){
// process each batch of records
}
global void finish(Database.BatchableContextbc){
// execute any post-processing operations
}
}
Create a batch class to update Contacts:
Description of Contact should be updated with
“Title of AccountName : Account’s Description“
Contact which does not have any Account associated with it, should not be updated.
Batch Class
===========
// Batch Class which implements Database.Batchable
global class UpdateContactDescription Implements Database.Batchable<sObject> {
// Getting all the contacts having some accountId populated using Query in
start() method
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'Select Id, Account.Name, Title, Description,
Account.Description From Contact Where AccountId != null';
return Database.getQueryLocator(query);
}
// Go through the each Contact and update the Contact's description
global void execute(Database.BatchableContext bc, List<Contact> scope) {
if(scope != null) {
for(Contact con : scope) {
con.Description = con.Title + ' of ' + con.Account.Name + ': ' +
con.Account.Description;
}
update scope;
}
}
// There is nothing as such required in finish method is it is empty but any
post process step can be added here
global void finish (Database.BatchableContext bc) {
Test Class
==========
Create Accounts (with some description) and Contacts associated with the Account
(without blank description).
Execute the batch Apex and verify that the Contacts Description should get updated
with some value.
@isTest
private class UpdateContactDescriptionTest {
@testSetup
static void dataSetup() {
// Insert Contacts with some description
List<Account> listOfAccounts = new List<Account>();
for(Integer i=0; i<10; i++) {
listOfAccounts.add(new Account(Name = 'TestAcc'+i,
Description = 'Account'+i + ' for Testing
Batch Apex'));
}
insert listOfAccounts;
// Contacts with some Account populated
List<Contact> listOfContacts = new List<Contact>();
for(Account acc : listOfAccounts) {
for(Integer j=0; j<5; j++) {
listOfContacts.add(new Contact(LastName = 'TestAccCon'+j,
AccountId = acc.Id,
Title = 'Sales Executive'));
}
}
// Contacts with no Account populated
for(Integer k=0; k<20; k++) {
listOfContacts.add(new Contact(LastName = 'NewCon'+k,
Title = 'IT Engineer'));
}
insert listOfContacts;
}
@isTest
static void testMethod_ContactsWithAccounts() {
// Verify that description is empty for all the contacts before the batch
class is executed
List<Contact> contactsBeforeUpdation = [Select Id, Description From
Contact];
for(COntact con : contactsBeforeUpdation) {
System.assert(con.Description == null, 'Description should not be
empty');
}
Test.startTest();
UpdateContactDescription batchClass = new UpdateContactDescription();
Database.executeBatch(batchClass, 100);
Test.stopTest();
// Assert that description should be updated for Contacts with Account
List<Contact> contactsAfterUpdation_Acc = [Select Id, Description,
Account.Name, Title From Contact Where AccountId != null];
for(Contact con : contactsAfterUpdation_Acc) {
System.assert(con.Description != null, 'Description should not be
empty');
System.assert(con.Description.contains(con.Account.Name), 'Description
should contain Account Name');
System.assert(con.Description.contains(con.Title), 'Description should
contain Contact Title');
}
// Assert that description should not be updated and remain empty for
Contacts with no Account
List<Contact> contactsAfterUpdation_noAcc = [Select Id, Description,
Account.Name From Contact Where AccountId = null];
for(Contact con : contactsAfterUpdation_noAcc) {
System.assert(con.Description == null, 'Description should not be
empty');
}
}
}