Queueable - Callout not allowed from this future method
You need to add Database.AllowsCallouts
to your class, not @future(callout=true)
. The error is erroneous.
public class getAccountUpdatesfromEndpoint implements queueable, Database.AllowsCallouts {
I would build an object and pass it into the queueable something like
public class AccountUpdateDates
{
public AccountUpdateDates(){}
public String startDate {get;set;}
public String endDate {get;set;}
}
Then for for your dates (how ever you do it, going to use your for loop logic)
Map<Integer, AccountUpdateDates> accountUpdatesDates = new Map<Integer, AccountUpdateDates>();
for(Integer i = 0; i < 20; i++)
{
AccountUpdateDates aud = new AccountUpdateDates();
Datetime endtimedatetime = now.addDays(-3*(monthIter-1));
String endtime = endtimedatetime.formatGmt('yyyy-MM-dd HH:mm:ss.SSS\'Z\'');
aud.endTime = endtime;
Datetime starttimedatetime = now.addMonths(-3*i);
String starttime = starttimedatetime.formatGmt('yyyy-MM-dd HH:mm:ss.SSS\'Z\'');
aud.startDate = startDate;
accountUpdatesDates.put(i, aud);
}
Then pass that entire object to your queueable, before you were constantly queueing jobs which kind of ruins the purpose of the queue
System.enqueueJob(new getAccountUpdatesfromEndpoint(accountUpdatesDates));
You will need to update your queueable to allow Callouts
implements Queueable, Database.AllowsCallouts
You can only enqueue up to 50 jobs at a time, you're doing 20 right now so you'd be okay but I still don't recommend doing it. Here is where it gets a little tricky, because you will need to figure out how to remove the dates from the Map<Integer, AccountUpdateDates>
as you do you service call out (I believe you can use the Integer key
here I was just spit balling). If you don't do this you will constantly be doing call outs on the first one in the Map
, also I recommend you create a "kill switch" in custom settings in order to break out of this because there is a risk that you will get stuck in a constant loop of job queueing
Then at the end of this you will need check something like
if(!accountUpdatesDates.isEmpty())
{
System.enqueueJob(new getAccountUpdatesfromEndpoint(accountUpdatesDates));
}
Remember you are removing the key/value pair after you do the call out. Also I would break this out into 3 different files the service
, queueable
, and the handler
that calls the queueable but that's a bigger/different discussion