[SalesForce] Queueable – Callout not allowed from this future method

I am using a queueable class to handle various date ranges that are then sent to a web service.

Upon calling the queueable class I am receiving the following error:

Callout not allowed from this future method. Please enable callout by annotating the future method. eg: @Future(callout=true).

When I try to add the @future method I receive the following error:

Future methods must be declared as static

I then add static and receive the following error:

Future methods do not support parameter type of
System.QueueableContext

Running the following in Execute Anonymous Window to test:

Datetime now = Datetime.now();
integer dayIter = 20; 
for(integer i = 0; i < dayIter; i++)
{
     datetime endtimedatetime = now.addDays(-3*(i-1)); 
     string endtime = endtimedatetime.formatGmt('yyyy-MM-dd HH:mm:ss.SSS\'Z\'');
     datetime starttimedatetime = now.addDays(-3*i);
     string starttime = starttimedatetime.formatGmt('yyyy-MM-dd HH:mm:ss.SSS\'Z\'');
     system.debug('starttime: ' + starttime);
     System.enqueueJob(new getAccountUpdatesfromEndpoint(starttime, endtime));
}

getAccountUpdatesfromEndpoint Class:

public class getAccountUpdatesfromEndpoint implements queueable {

public string receiveStartTime;
public string receiveEndTime;

public getAccountUpdatesfromEndpoint(string startTime, string endTime) {
    receiveStartTime = startTime;
    receiveEndTime = endTime;
    }

public void execute (Queueable Context context){

string jsonstr = '';
List<Custom_Object__c> accList = new list<Custom_Object__c>();

    String starttime = receiveStartTime;
    String endtime = receiveEndTime;

     HttpRequest request = new HttpRequest();
     .....

     try {
         .....
         jsonStr = res.getbody();             
         JSONConvertEndpoint endpointGet = (JSONConvertEndPoint)JSON.deserialize(jsonStr, JSONConvertEndPoint.class);

         for(integer i=0; i < endPointGet.size(); i++) {
             Custom_Object__c acc = new Custom_Object__c();
             acc.Field__c = endPointGet[i].field;
             accList.add(acc);
         }

         upsert accList;


     }

   catch {
          /* exception logging */
         }


}

Here is the link to my previous stack question: Schedule Iteration of Webservice Callouts

Best Answer

You need to add Database.AllowsCallouts to your class, not @future(callout=true). The error is erroneous.

public class getAccountUpdatesfromEndpoint implements queueable, Database.AllowsCallouts {
Related Topic