You need to sign in to do that
Don't have an account?

Incorrect error message? You have uncommitted work pending.
Hi everyone,
I've been altering some integration code and now I get the dreaded "You have uncommitted work pending. Please commit or rollback before calling out" message. This is supposed to indicate I have begun an update or insert and subsequently made a callout.
The code I started with is very OO and it's hard to figure out exactly what is getting run when. So after a while I started copying the code into a refactored base so I can tell exactly what is going on. I am now at the point where my code has no DML at all, I'm not even doing a select and I still get the error. Code is below.
I have to think one of two things is happening. Either the error message is incorrect or something is "stuck." I don't have solid evidence for the latter idea but this error message has come and gone a couple of times without any obvious action on my part.
As I search this error message I only find people who actually were doing DML before the callout. Clearly I am not. Am I special? I hope someone else has faced this.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// execute this line in an anonymous block to see the error non-fun
CalloutLaidBare.toddWillMakeItHappen();
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// here's the class
public with sharing class CalloutLaidBare {
public static void toddWillMakeItHappen() {
Map<String, String> params = new Map<String, String>();
params.put('master_plan_no', '10266411');
params.put('master_plan_units', '1');
// etc, all hard-coded values
// normally I'd be parsing values from an opportunity which is why this is seperate from the future method
String qs = makeStringFromMap(params); // this works
CalloutLaidBare.makeItHappenCallout(qs);
}
@future(callout=true)
public static void makeItHappenCallout(String qs) {
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('hard-coded endpoint');
req.setMethod('POST');
req.setBody(qs);
try {
HttpResponse res = h.send(req);
// why bother parsing, I'll never get here
} catch (Exception e) {
// email myself e.getMessage() and drown my sorrows in chocolate
}
}
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
What instance are you on?
I'm doing it on a Winter '12 instance and it is working just fine. Are you getting the error in the method you're calling from the System Log or are you getting it in the async method?
All Answers
Just to double check, can I see what's happening in MakeStringFromMap(params)?
Sure, here it is:
private static String MakeStringFromMap(Map<String, String> params) {
String res = '';
for (String key : params.keySet()) {
res += key + '=' + EncodingUtil.urlEncode(params.get(key), 'UTF-8') + '&';
}
res = res.substring(0, res.length() - 1);
return res;
}
The result was:
postal_cd=&phone=&address1=&bill_city=San+Diego&bill_postal_cd=&city=Del+Mar&first_name=&bill_locality=&bill_address1=Var+002&status_cd=1&company_name=&master_plan_units=1&bill_first_name=&supp_plan_units=1&resp_level_cd=&master_plan_no=10266411&bill_last_name=¬ify_method=&country=&pay_method=-1&email=&bill_company_name=&last_name=&supp_plans=10266415&bill_country=&locality=
What instance are you on?
I'm doing it on a Winter '12 instance and it is working just fine. Are you getting the error in the method you're calling from the System Log or are you getting it in the async method?
I am on Summer, cs3. But you gave me an idea.
I checked the meta xml file and it was the default value (16.0). I changed it to 22.0 and my super bare-bones code is now running!
Thanks for this! I will start putting it back together and see the error returns. If it does I'll see if I have another v16 class out there.
Can you help me how you seperated dmls and callouts in your code. i am also facing the same issue . Please see my link.
https://developer.salesforce.com/forums/?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000BKhfIAG