I have an endpoint I am getting updates from, but the only arguments the end point takes is two date parameters. To keep the heap size down, I am limited to about a date range of three months.
I am then using the response body to update objects.
My issue:
You have uncommitted work pending. Please commit or rollback before calling out
Some research into this issue had resolutions utilizing actionFunctions and onComplete's but then I believe I would not be able to schedule this. Another solution to a similar problem was adding the @future method, but then I end up with an error that future methods cannot be called from future methods.
Scheduler Class:
public class getAccountUpdatesScheduler implements Schedulable{
public void execute (SchedulableContext SC)
{
getAccountUpdatesfromEndpoint.GetAccountUpdatesEndpoint();
}
}
getAccountUpdatesfromEndpoint Class:
public class getAccountUpdatesfromEndpoint {
@future(callout=true)
public static void GetAccountUpdatesEndpoint(){
Datetime now = Datetime.now();
integer monthIter = 20;
string jsonstr = '';
List<Custom_Object__c> accList = new list<Custom_Object__c>();
for(integer i = 0; i < monthIter; i++) {
datetime endtimedatetime = now.addDays(-3*(dayIterCount-1));
string endtime = endtimedatetime.formatGmt('yyyy-MM-dd HH:mm:ss.SSS\'Z\'');
datetime starttime = now.addMonths(-3*i);
system.debug('starttime: ' + starttime);
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 */
}
}
}