Yes you can call one batch job from another batch job. This is the best practice to call 2nd batch in 1st batch job finish method.Like belowSaveBatch batchJob = new SaveBatch();Id batchInstanceId = Database.executeBatch(batchJob, 200);Sample code for you :- global class SearchAndReplace implements Database.Batchable<sObject>
{
global final String Query;
global final String Entity;
global final String Field;
global final String Value;
global SearchAndReplace(String q, String e, String f, String v)
{
Query=q; Entity=e; Field=f;Value=v;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
for(sobject s : scope){
s.put(Field,Value);
}
update scope;
}
global void finish(Database.BatchableContext BC)
{
SaveBatch batchJob = new SaveBatch();
Id batchInstanceId = Database.executeBatch(batchJob, 200);
}
}
Please mark this as solution if this will help you.
https://www.sundoginteractive.com/sunblog/posts/calling-one-salesforce-batch-job-from-another-batch-jobThanksAmit Chaudhary
1 件の回答