3

I am facing this issue in a MockCallOut for http in test class. I know that a dml is not allowed just above the callout, so I have tried to put callout between Test.startTest() and Test.StopTest(); I have also tried doing the callout in future method. But, still I am unable to solve this problem. I need this dml as I need to insert data that is used in calling the method. Below is the abstract information about my implementation:

static testMethod void methodForMockCallOut(){      
    Account acc = new Account(name='Test Account');
    insert acc;

    Contact con= new Contact();
    con.FirstName = 'Test';
    con.LastName = 'User';
    con.AccountId = acc.Id;      
    insert con;

     test.startTest();
     Test.setMock(HttpCalloutMock.class, new myMockCallOutClass());
     MockCallOutForActualMethodToBeTested(con.id);
     Test.stopTest();
}

@Future(Callout=true)
MockCallOutForActualMethodToBeTested(Id ContactId){
      ClassToBeTested obj = new ClassToBeTested();
      obj.actualMethodToBeTested(con.id);
}
  • 1
    It would be great if you post your code! Especially the test class code... – Amit Bangad Dec 1 '14 at 6:46
  • Hi Amit, thanks for the response. I have added the test class code. – SfdcNovice Dec 1 '14 at 8:46
  • Do you have a callout (HTTP Request) in a for loop by any chance? – Amit Bangad Dec 1 '14 at 9:25
  • No, Callout from test is not there in for loop. But, there are multiple callouts from the method which is being tested – SfdcNovice Dec 1 '14 at 10:04
  • So I think the real issue is ... there is a DML then callout then DML again then callout again....... right? – Amit Bangad Dec 1 '14 at 10:06
1

There were several Salesforce issues related to

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

Besides of the issues mentioned, additional issues that I have encountered with, were:

  1. The problem with @future(Callout=true) methods. First time I reported this was at February 12, 2015 Spring 15 Release (v.33)

  2. There was a known issue, which was claimed to be fixed in the next year, however, it wasn't really fixed. I have opened another Salesforce support case, they told me the work id of it was "W-3171561" but they never provided a list to known issue and I was promised to be notified once this issue is resolved, however, I was never notified on this issue resolution.

  3. Later I suggested a workaround for this long-running issue to downgrade the apex class version to 28.

  4. As of Summer 2019 release (version 46) I can't reproduce the future callout issue anymore. However, I have found another issue, the reasons for which are not clear to me. It is somehow related to user trigger and batch or queueable jobs. I have opened another Salesforce support case for this. Let's see how this goes. I have posted about this issue today on my blog

  5. The issue with user insert and future seems to be fixed for future jobs but not for queueable jobs.

Conclusion.

There is some constant long-running ever-lasting bug related to uncommitted work pending. It is hard to find and reproduce it. Every time when Salesforce makes some change to this implementation, it becomes harder to debug and to troubleshoot it, but the core issue is still present.

Some workaround might be applicable for this issue.

  1. Try to downgrade the class version to 28.0 or older where issue wasn't introduced yet (the version should be older than Spring 15).

  2. Try to move the setup code into method annotated with @testSetup annotation. It is not clear why, however, in many cases this helps to workaround this issue

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.