function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gaurav Sinha 27Gaurav Sinha 27 

Callout in Batch Apex

Hi all Veterans
We have a requirement wherein we have to sync the account record with an external server. In account there would be a checkbox to select to sync, if the checkbox will be selected the sync should be processed asynchronously and after the sync is successfully done the checkbox should be set to false. I would appreciate if someone can help me with the complete code.
Thanks in advance.
Best Answer chosen by Gaurav Sinha 27
Ajay K DubediAjay K Dubedi
Hi Gaurav ,

All future methods and Batch Classes are Asynchronous and we can not call an Asynchronous call from an Asynchronous call.
Yes, you can call (A queueable class method acts as a future method) only from the Batch class finish method. You can implement a Queueable class that acts as a Future method and then you can invoke another future method. This is an indirect way of calling a future method from the future method (A queueable class method acts as a future method). But this approach also has a governor limit of 2.
So at max, you can call two future methods.
Batch Class:
global class NewBatchClass implements Database.Batchable<Account>,Database.AllowsCallouts{
 global Iterable<Account> start(Database.BatchableContext bc) {
    return [select id,name from account ];
 }

    global void execute(Database.BatchableContext BC, List<Account> scope)
        {
            
        }

    global void finish(Database.BatchableContext BC)
        {
            
                TestBatch tt = new TestBatch();
            System.enqueueJob(tt);
            
            
        }

}
Future-Queuable class:
public with Sharing class TestBatch implements Queueable, Database.AllowsCallouts {


    public void execute(QueueableContext context) {
    }

}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi