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
Stefano AmentaStefano Amenta 

Too many callouts with Apex Batch

Hi,
I wrote an apex batch which helps to delete reports in the Public Folder that haven't been run for more than 60 days.

When I test it in the Dev Console, I get an error such as "Too many callouts: 101".

How can I improve the code to overcome this issue?

Thanks for the help.
 
global class DeleteOldReportsBatch implements Database.Batchable<sObject>, Database.AllowsCallouts {

    //Get the records to be deleted
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'select id, FolderName from Report where LastRunDate <= LAST_N_DAYS:60';
        return Database.getQueryLocator(query);        
    }

    //Executes the deletion logic for reports records
    global void execute(Database.BatchableContext BC, List<Report> scope) {
    String url;

    for(Report r : scope) {
        if (r.FolderName == 'Public Reports'){        
            Http h = new Http();
            url = '/services/data/v44.0/analytics/reports/' + r.Id;
            System.debug(LoggingLevel.INFO, '*** url: ' + url);          

            HttpRequest req = new HttpRequest();

            req.setHeader('Authorization', 'Bearer '+ UserInfo.getSessionId());
            req.setMethod('DELETE');
            req.setEndpoint(url);

            HttpResponse res = h.send(req);
            System.debug(LoggingLevel.INFO, '*** rest: ' + res.getBody());
        }
        } 
    }

    global void finish(Database.BatchableContext BC) {}
}

 
Best Answer chosen by Stefano Amenta
Ajay K DubediAjay K Dubedi
Hi Stefano,
You need to update your Scheduler code :
public class SchedulerForPostApex implements Schedulable{
    public void execute(SchedulableContext sc){
       
        Postbatchclass  batchapex = new Postbatchclass();
        id batchprocessid = Database.executebatch(batchapex,100);
 
    }
}
Used this code it will work fine.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Stefano,
You need to update your Scheduler code :
public class SchedulerForPostApex implements Schedulable{
    public void execute(SchedulableContext sc){
       
        Postbatchclass  batchapex = new Postbatchclass();
        id batchprocessid = Database.executebatch(batchapex,100);
 
    }
}
Used this code it will work fine.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
AbhimanyuAbhimanyu
I think you are executing batch with default size which is 200, you can only make 100 callouts in an Apex Transaction.
I developer console try reducing the batch size to 100.

Id batchId = Database.executeBatch(new DeleteOldReportsBatch(), 100);

ref. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

Mark it as best answer if it helped.
Stefano AmentaStefano Amenta
Thanks Ajay and AbhimanyuD3DX

It works fine now!