0

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 */
         }

    }
}
3
  • Please try with batch you can call another batch from a batch Apr 27, 2018 at 19:26
  • 1
    You can use a queueable.. That error you're getting is something else
    – EricSSH
    Apr 27, 2018 at 19:30
  • @EricSSH , queueable kind of makes sense. So I would schedule a class that executes my "for" class that iterates my date ranges and sends each iteration to a queueable class to handle the web service callout?
    – S.B.
    Apr 27, 2018 at 19:48

1 Answer 1

0

The reason you are getting that Uncommitted work error is because you are trying to upsert your accList and then doing another call out (move that upsert out of the for loop). In your comments we talked about a queueable would work for this so if you want to go that route read up on queueables and possibly ask another question..

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

2
  • See the following new post: link Continuing to encounter different, but future related errors.
    – S.B.
    Apr 27, 2018 at 20:36
  • Accept this one if you're moving on to the next one please
    – EricSSH
    Apr 27, 2018 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .