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
salesforce sfdxsalesforce sfdx 

Hi Team, Getting the Callout Error while scheduling the apex class with a Batch

I have to covert the report to csv and   schedule a apex  class to run every 5mintes :
i had tried using a batch, 
First error: Too many callouts: 1.
please let me know your comments if any other approach:
BatchApex:
global with sharing class SampleBatchApexClass implements Database.batchable<sObject>
{
   //Retrieve only the records which needs to be processed
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        //Starting query goes here
        String query = 'SELECT Id,Name FROM Account limit 1';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<sObject> scope)
    {
        //Batch class business logic goes here
        //
       
    }
        
    global void finish(Database.BatchableContext bc)
    {
        
         List<Messaging.EmailFileAttachment> attachments=new List<Messaging.EmailFileAttachment>();
            List<Report>reportList = [SELECT Id,DeveloperName,Name FROM Report where DeveloperName ='flow_screen_prebuilt_report' limit 1];
            if(reportList.size()>0){
                for(Report report:reportList){
                    String reportId = (String)report.Id;     
                    string reportName=(String)report.Name;     
                    ApexPages.PageReference objPage = new ApexPages.PageReference('/'+reportId+'?csv=1&isdtp=p1');                    
                    Messaging.EmailFileAttachment objMsgEmailAttach = new Messaging.EmailFileAttachment();
                    Date dt = System.today().toStartOfMonth().addMonths(-1);
                    System.debug(dt.format());
                    objMsgEmailAttach.setFileName(reportName+' '+dt.format()+' '+'.csv');
                    //if(!Test.isRunningTest())
                    objMsgEmailAttach.setBody(objPage.getContent());
                    objMsgEmailAttach.setContentType('text/csv');
                    attachments.add(objMsgEmailAttach);
                }
            }  
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           
            mail.setUseSignature(false);
            mail.setToAddresses(new List<String>{'test.test@gmail.com'});
            mail.setSubject('Daily Report from Salesforce');
            mail.setHtmlBody('<br/><br/>Please review daily reports attached.<br/><br/><br/><br/>');
            mail.setFileAttachments(attachments);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        //here scheduling starts
        Datetime dt = system.now().addMinutes(5);
        String day = string.valueOf(dt.day());
        String month = string.valueOf(dt.month());
        String hour = string.valueOf(dt.hour());
        String minute = string.valueOf(dt.minute());
        String second = '00';
        String year = string.valueOf(system.now().year());
        String strJobName = 'SampleSchedulingClass  -' + year + '-' + month + '-' + 
        day + ' ' + hour + ':' + minute + ':' + second;
        String strSchedule = '00 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        System.schedule(strJobName, strSchedule, new SampleSchedulingClass  ());
    }
}

Schedulable class:
global class SampleSchedulingClass  implements Schedulable 
{
    global void execute(SchedulableContext ctx) 
    {
        SampleBatchApexClass renewalOppBatch = new SampleBatchApexClass();
        Database.executeBatch(renewalOppBatch, 5);    
        System.abortJob(ctx.getTriggerID());
    }
}

ConExpression for starting:
Datetime dt = system.now().addMinutes(2);
        String day = string.valueOf(dt.day());
        String month = string.valueOf(dt.month());
        String hour = string.valueOf(dt.hour());
        String minute = string.valueOf(dt.minute());
        String second = '00';
        String year = string.valueOf(system.now().year());
        String strJobName = 'SampleSchedulingClass  -' + year + '-' + month + '-' + 
        day + ' ' + hour + ':' + minute + ':' + second;
        String strSchedule = '00 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        System.schedule(strJobName, strSchedule, new SampleSchedulingClass  ());

 
Best Answer chosen by salesforce sfdx
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you add   Database.AllowsCallouts  as well in batch class and check it as shown below..
 
global with sharing class SampleBatchApexClass implements Database.batchable<sObject>,Database.AllowsCallouts
Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you add   Database.AllowsCallouts  as well in batch class and check it as shown below..
 
global with sharing class SampleBatchApexClass implements Database.batchable<sObject>,Database.AllowsCallouts
Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
salesforce sfdxsalesforce sfdx
Thank you 
Sai Praveen