0

I understand that I cannot directly call a future method from a batch class. But from many other answers, I can see that it is possible to do so by creating a helper class and calling the future method there. But it is not working for me. Please check my code below. Also, I have tried to do it with the queueable class as suggested in this link, but it is not working for me. The error was " Callout not allowed from this future method. Please enable callout by annotating the future method. eg: @Future(callout=true)"

But I am more interested in the first and simpler way to do this.

public class OrdersItemsHelper {
    static Document tDoc;
    static blob csvBlob; 

    //prepare csv file to send
    public static void CreateCsvFile(List<Order_Line_Items__c> orderItemsList)
    {  

       //Code to create file here

        csvBlob = Blob.valueOf(finalstr);
        tDoc = new Document();
        tDoc.Name = 'sales_items_' +date.today();
        tDoc.Type = 'csv';
        tDoc.body = csvBlob;
        tDoc.FolderId = [select id from folder where name = 'Emarsys Order Files'].Id;
        tDoc.ContentType = 'application/vnd.ms-excel';
        Insert tDoc;
        system.debug('doc inserted');

        sendFile();

    }


    @Future(callout = true)
    public static void sendFile()
    {
        System.debug('I am creating the post request');
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setMethod('POST');
        request.setHeader('Authorization','Security Token');
        request.setHeader('Accept','text/plain');
        request.setHeader('Content-Type', 'text/csv');
        request.setHeader('Authorization', 'Bearer ');
        request.setBodyAsBlob(csvBlob);
        HttpResponse response = http.send(request);
        system.debug('response: ' + response);
    }
Faiza Iqbal
  • 191
  • 2
  • 11

1 Answers1

0

So I tried again by doing it in the queueable apex class. The thing I was missing was "Database.AllowsCallouts" in the class heading. Below is my queueable class which is working with the batch class to send a rest post request.

    public class OrderItemFilePostHelper implements System.Queueable,Database.AllowsCallouts
{
private Blob csvBlob;

    public EmarsysOrderItemFilePostHelper(Blob csvBlob) {
        this.csvBlob = csvBlob;
    }

    public void execute(System.QueueableContext objContext)
    {
        System.debug('I am creating the post request');
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setMethod('POST');
        request.setHeader('Authorization','Security Token');
        request.setHeader('Accept','text/plain');
        request.setHeader('Content-Type', 'text/csv');
        request.setHeader('Authorization', 'Bearer ');
        request.setEndpoint('https://webhook.site/b0746268-e95c-4f94-bcb6-61d4bea54378');

        request.setBodyAsBlob(csvBlob);
        HttpResponse response = http.send(request);
        system.debug('response: ' + response);
    }

}
Faiza Iqbal
  • 191
  • 2
  • 11