Before API version 26.0 it was not possible to start another batch job from an existing batch job. Starting with API version 26.0, you can start another batch job from an existing batch job to chain jobs together.
Chain a batch job is useful when you want to start a job
after another one finishes and when your job requires batch processing, such as
when processing large data volumes. Otherwise, if batch processing isn’t
needed, consider using Queueable Apex.
Let’s us understand with a real time example of how to
invoke a batch apex from another batch apex.
The provided code defines two Salesforce Apex batch classes,
BatchClassToUpdateAccountIndustry and BatchClassToUpdateContactIndustry,
demonstrating a sequence of data processing tasks.
The provided code represents two Salesforce Apex batch
classes designed to update the Industry field on Account and Contact records.
Here's a short description of the code:
These batch classes work together to update the Industry
field on Account where Type is Technology Partner and all contacts where Account
Industry is Communications, ensuring that all Contacts have Industry as Communications
where Account Industry is Communications. The second batch class is invoked
from the finish method of the first batch class to maintain the sequence of
updates.
global class
BatchClassToUpdateAccountIndustry implements Database.Batchable<sObject>
{
String query = 'Select Id,Name,Rating,
Industry,Type from Account WHERE (Type = \'Technology Partner\') ';
String accIndustry='Communications';
List<Account> upddateAccount= new
List<Account>();
global Database.QueryLocator
start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext
bc, List<Account> records){
System.debug('records'+records.size());
for (Account accountobj : records) {
accountobj.Industry = accIndustry;
upddateAccount.add(accountobj);
}
try{
update upddateAccount;
}
catch(Exception e){
System.debug('Error is
'+e.getMessage());
}
}
global void finish(Database.BatchableContext
bc){
//Note: We are calling second batch class i.e
BatchClassToUpdateContactRating from Finish Method of this batch.
BatchClassToUpdateContactIndustry b = new BatchClassToUpdateContactIndustry
();
Database.executeBatch(b, 200);
}
}
global class BatchClassToUpdateContactIndustry
implements Database.Batchable<sObject> {
List<Contact>
updatedContacts= new List<Contact>();
String query = 'Select FirstName,
LastName,Account.Rating from Contact WHERE (Account.Industry =
\'Communications\') ';
global Database.QueryLocator
start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext
bc, List<Contact> records){
for(Contact con : records){
con.Industry__c ='Communications';
updatedContacts.add(con);
}
update updatedContacts;
}
global void finish(Database.BatchableContext
bc){
}
}
Now, let’s test the working of the above batch classes.
Open execute anonymous window and paste the below code and
click execute.
BatchClassToUpdateAccountIndustry batchObj= new
BatchClassToUpdateAccountIndustry();
Database.executebatch(batchObj,200);
Now, we can verify one of the account where the type is technology
partner to check if account industry and contact industry is updated or not as
shown below.
No comments:
Post a Comment