5
Savepoint sp = Database.setSavepoint();
insert new Campaign(Name='test');
Database.rollback(sp);

HttpRequest req = new HttpRequest();
req.setEndpoint('http://google.com');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = 
    http.send(req);
system.debug(req);

If you paste the above code into an anonymous block and execute, you will get the following error message:

System.CalloutException: You ahve uncommitted work pending. Please commit or rollback before calling out

However, as you can see, I am rolling back on the third line there. Am I missing something?

4

You're not able to do that, you can't do an HTTP callout either after a DML operation or a setting of a Savepoint. Documentation here mentions DML, but not the Savepoint that I can see, but that is the case.

  • 2
    Using SavePoint does cost a DML operation, however (of the 150/transaction variety). Transaction Control explains this: "Each savepoint you set counts against the governor limit for DML statements." – sfdcfox Mar 30 '15 at 19:54
  • 2
    The error message says "Please commit or rollback before calling out." How, then, am I supposed to rollback to do my callout? – mikegedelman Apr 1 '15 at 14:09
  • Why are you rolling back anyway, what would be the point of that? I think in light of what limitations there are around this, you'll need to refactor your code. What is it you're trying to achieve? – Phil Hawthorn Apr 1 '15 at 14:11
  • I'm rolling back because of an error condition in a future method, which is invoked by a REST service I've exposed. Client has an endpoint that I can hit with an error message, so the intention is to roll back and then call out to notify of the error. – mikegedelman Apr 7 '15 at 14:05
  • Yeah thats going to be tricky without some refactoring. Here's an interesting post that has some creative solutions salesforce.stackexchange.com/questions/54931/… – Phil Hawthorn Apr 7 '15 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.