Get a Quote

Hey, do you want to share your amazing ideas with us, drop your requirements, we are keen to work with you on your fantastic project. We will try to reach you shortly, SEE YOU !

Tell us about you

Tell us about your Project

How To Invoke Batch Apex From Another Batch Apex In Salesforce

How is it possible?

There are two ways in salesforce which are used to call the batch class from another batch class are:

  • Using Queueable Apex
  • Using the Finish method of Batch class.

Using this way, you can create the chaining between the batches.

Note: If you call the batch class from start or execute methods then Salesforce throws the below error:
System.AsyncException: Database.executeBatch cannot be called from a batch start, batch execute, or future method.

Also Read: How to Implement Salesforce SSO Using OAuth?

While calling batch from the start method there is an error:batch execute error

Same error while calling from execute method of batch:execute method of batch

Here is the complete definition of error message:complete definition of error message
Also Read: How To Connect Pipedrive and Salesforce Integration?

There are two solutions for this error:

1. Using Queueable Apex:

Queueable Apexusing Queueable Apex

2. Using Finish Method of Batch Class:

Batch Class Example:

Finish Method of Batch Class
global class BatchAccountUpdate implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
String query = 'Select Name from Account WHERE Name != null AND (Name = \'Sapna Company\' OR Name = \'Sapna\') ';
global Database.QueryLocator start(Database.BatchableContext bc) {
// collect the batches of records or objects to be passed to execute
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext bc, List<Account> records){
// process each batch of records
for(Account acc : records){
acc.Name = acc.Name + ' - Updated';
}
update records;
}

global void finish(Database.BatchableContext bc){
// execute any post-processing operations
Note: We are calling Batch from Finish Method of Second batch.
BatchUpdateAccountRelatedContacts b = new BatchUpdateAccountRelatedContacts();
Database.executeBatch(b, 200);
}
}

hire salesforce developers

Calling from Schedule Class:

global class ScheduleBatchAccountUpdate implements Schedulable {
global void execute(SchedulableContext sc) {
BatchAccountUpdate b = new BatchAccountUpdate();
database.executebatch(b);
}
}

Second Batch:

global class BatchUpdateAccountRelatedContacts implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
String query = 'Select FirstName, LastName, Email, Status__c from Contact WHERE
FirstName != null AND LastName != null AND AccountId != null';
global Database.QueryLocator start(Database.BatchableContext bc) {
// collect the batches of records or objects to be passed to execute
Note: Calling Queueable job for calling the next batchfor chaining.
System.enqueueJob(new ExecuteBatchQueueableJob());
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Contact> records){
// process each batch of records
for(Contact con : records){
con.Status__c = 'Discovery Call Complete';
}
update records;
}
global void finish(Database.BatchableContext bc){
// execute any post-processing operations
}
}

Queueable Apex:

public class ExecuteBatchQueueableJob implements Queueable {
public void execute(QueueableContext context) {
try{
BatchCalledThroughQueueable b = new BatchCalledThroughQueueable();
Database.executeBatch(b, 200);
}catch(Exception e){
System.debug('@@@ Error Message : '+e.getMessage());
}
}
}

Third Batch:

global class BatchCalledThroughQueueable implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
String query = 'Select FirstName, LastName, Email, Status, Title from Lead WHERE FirstName != null AND LastName != null';
global Database.QueryLocator start(Database.BatchableContext bc) {
// collect the batches of records or objects to be passed to execute
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Lead> records){
// process each batch of records
for(Lead ld : records){
ld.Title = ld.FirstName + ' - ' + ld.LastName;
}
update records;
}
global void finish(Database.BatchableContext bc){
// execute any post-processing operations
}
}

Also Read: How to Create a Salesforce Lightning Map Component?

author profile
About Author
Author Name:
Virendra Sharma

Certified Magento 2 Consultant

Author Bio:

Virendra Sharma is an ecommerce tech-enthusiast and certified Magento Developer. With a decade of experience in Magento ecommerce development, he has been assisting ecommerce businesses across the globe to harness Magento capabilities by developing, maintaining, and improving clients’ eCommerce websites. A leader in his own rights his teammates see him as an avid researcher and a Magento evangelist.

About Emizentech:

An acclaimed IT solution provider and a leading Magento 2 development company with its expertise lies in delivering top of the line ecommerce solutions, Salesforce commerce cloud services, mobile app development and many more verticals.

Relate Contact to Multiple Contacts
27 May, 2020

How To Relate a Contact To Multiple Accounts In Salesforce

With the Salesforce Summer ’16 release, you can relate a single contact to multiple accounts, without duplication! This is a […]
Read more
Salesforce Development
Chatbots in eCommerce Industry
27 May, 2020

How AI Chatbots Are Benefiting The eCommerce Industry?

Whether you call it a revolution or an intrusion, chatbots have gained a significant place in the ecommerce practices. From […]
Read more
Ecommerce Development
Odoo ERP Integration In eCommerce Platform
26 May, 2020

Odoo ERP Integration In eCommerce Platform: Complete Guide

Who knew that shopping online within just a few clicks would be easier than preparing a cup of tea. Ecommerce […]
Read more
Ecommerce Development

Let’s Talk Business

Reach out to us by following below given details, or simply drop us a text to start a conversation, we are here to provide best in class business solutions.

SAY SOMETHING