I am getting the following error, and I find it pretty odd as 2 days ago I deployed and it worked fine, and my current deployment doesnt have anything to do with the class that is now failing. To make it more bizare, the sandbox test class works fine (I am not using any local data in the test)

the error message is: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

after a lot of research what i found was that DML statement should be done after a callout, and my class is not an exception the update of the record is done after the api is call, so i dont know why today is just not working.

my test look like this:

    Test.setMock(HttpCalloutMock.class, new VedaMockHttpResponseGenerator());
    //this is the section that is causing the break:

    btCtrl.opp.Amount = null;

    Test.setMock(HttpCalloutMock.class, new MinItAddClientMockHttpResponseGenerator());

    btCtrl.opp.Min_IT_Client_ID__c = '123';
    btCtrl.opp.Min_IT_Loan_ID__c   = '123';
    btCtrl.opp.Min_IT_Client_ID__c = null;

the class looks something like this

    //------------- Main Process ---------------------
    xmlRequest = generateXmlEnvelope(environment, oppId, conInfo, GenderCode, StateCode, LoanTypeCode, LoanAmt);
    //System.debug('##xmlRequest: '+xmlRequest);
    ResponseBody = callAPI(environment, xmlRequest);
    returnMessage = checkError(ResponseBody);
    if(returnMessage != null) {
        returnMessage = returnMessage;
    } else {
        returnMessage = SaveAttachment(ResponseBody, oppId);

SaveAttachment is the method that contains the DML Statement of insert


It was too long for a comment, so going to leave it as an answer.

I've had a similar problem a while ago. The code was executing without any problems on some sandboxes, but failing on the other ones, even though I did not touch that code and it was identical(including all classes/methods) that were called by it.
It did appear like Test.setMock() was not executed properly and the test class was still trying to make an actual callout.

The problem was apparently in a workflow with email alert that was triggered by my test data and failing the test class. From your question I understood that this code did work good 2 days ago and it's just failing now.
Can you take a look at your workflows/processes/other point-and-click stuff? Disable the ones that were created in the past 2 days and run it again. If the test class passes - start activating these features and you will most probably find the one that causes your test class to fail

| improve this answer | |
  • You were right. It was a workflow that was trying to send an email – manza Apr 10 '16 at 23:51
  • Just saw this exact problem in a sandbox, this was the answer. Thank you, @Novarg, just saved me a ton of troubleshooting – mscholtz Apr 15 '16 at 0:52

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.