Skip to main content
can someone provide me the syntax to call another batch from one batch in batch Apex and a sa,ple code to check programatically if possible.

 
1 réponse
  1. 28 mai 2015, 15:49
    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 below

    SaveBatch 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-job

    Thanks

    Amit Chaudhary

     
0/9000