0

I have a batch that does callouts. This batch when I tried to schedule, gave me an error "Callouts not supported from Schedule class". I thought about the future, but that does not work as the future cannot call batch. So, I took an approach where I publish a platform event and implemented a platform event trigger. This platform event trigger does the job of calling the batch which does the callout. I took the approach of firing platform event as platform events are asynchronous and thought they provide me a way to call batch from platform event trigger. My batch which talks to connect API is not working as the user context is getting changed to Automated Process.

Below is the code for reference.

Scheduler class:

global class EinsteinJobScheduler implements Schedulable{
    global void execute(SchedulableContext SC) {
       List<Einstein_Alerts_Composer__c> Ecomposers = [select Id,Name from Einstein_Alerts_Composer__c];
       List<Einstein_Payload_Event__e> EPayloads = new List<Einstein_Payload_Event__e>(); 
        for(Einstein_Alerts_Composer__c ec:Ecomposers){
           EPayloads.add(new Einstein_Payload_Event__e(EinsteinComposerName__c=ec.Name));
        }
        if(EPayloads.size()>0){
            system.debug('In the Publish Logic');
            EventBus.publish(EPayloads);//Publishing Platform Events
        }
        
    }
}

Einstein Payload Event Trigger

trigger EinsteinPayloadEvent on Einstein_Payload_Event__e (after insert) {
    System.debug('new Size '+trigger.new.size());
    for(Einstein_Payload_Event__e EinsteinPayload :trigger.new){
       if(EinsteinPayload.EinsteinComposerName__c!=null){
           EinsteinService sch1 = new EinsteinService(EinsteinPayload.EinsteinComposerName__c);
           Database.executeBatch(sch1,1); 
       }
    }
}

Batch Logic Which is Failing

ConnectApi.LiteralJson queryresult =  ConnectApi.Wave.executeQuery(ganSAQL);
queryResponse = queryresult.json;
  • 1
    a schedulable can start a batchable and if the batchable implements Database.AllowCallouts, then callouts can be done – cropredy Aug 18 at 17:38
  • I don`t think it can do it. Callouts are not allowed from Scheduler – Shiva Achukatla Aug 19 at 14:29
  • schedulable invokes batchable; batchable does callouts; schedulable could invoke queueable, queueable could do callouts – cropredy Aug 19 at 17:32
  • @cropredy Each Batch callout does a DML. So sequential Batch is resulting in a "uncommitted Work Pending Error". Hence I am doing a platformevent and the platform event trigger takes care of queueing the batch. Platformevents are asynchronous. I have discovered one more thing. Platformevents are firing but my batch which has logic to talk to connect API is failing as user context is getting changed to "Automated Process" – Shiva Achukatla Aug 20 at 12:30
  • @cropredy I changed my question title – Shiva Achukatla Aug 20 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.