+ Start a Discussion
Rishiraj Singh 19Rishiraj Singh 19 

what is the best practice to invoke rest webservice after a batch job gets completed successfully?

My requirement is once all the records are updated using Batch, all the updated records should get synced with the external System using REST services.
PawanKumarPawanKumar
As you are processing data in batch so you will have volume data to send to the external system as well. so I would suggest you call another batch in finish() method your batch.

------------------------
global class CalloutsFromBatchExample implements Database.Batchable<SObject>,Database.AllowsCallouts,Database.stateful{

    public Boolean chainAnotherbatch = true;

    public void OwnLocalBatchClass(){
    }   

    public Iterable<SObject> start(Database.BatchableContext bc){

        Integer totalCallouts = Limits.getLimitCallouts();

        for(Integer i = 0;i<totalCallouts;i++){
            //Make the callout
            //Build the scope object
            //Have a if condition to set the chainAnotherbatch boolean to true or false.            
        }
        return scope;
    }

    public void execute(Database.BatchableContext bc, List<Sobject> scope){
            Database.insert(scope); 
    }

    public void finish(Database.BatchableContext bc){
        if(chainAnotherbatch){
            Database.executeBatch(new CalloutsFromBatchExample(),1000);
        }else{
            System.debug('Start method signalled that no need to chain another batch another batch job ');
        }
    }
}
-----------------------

https://salesforce.stackexchange.com/questions/88890/calling-batch-from-finish-of-same-batch-job

Please mark it best if it helps you. Thanks.
Rishiraj Singh 19Rishiraj Singh 19
Hi Pawan,
Is it a good way to call out in start even there is uncertainity of batch execution successful. In case Batch fails to execute, Still call out will sync the data to external system but in master system data wont be updated.