10

Seeing this error now if an object has an email alert on it during insert and using test.setMock

Disabling the workflow/process builder that contains the email alert and the test passes fine.

Test.setMock is inside or outside test.startTest() (does not matter). Test.starTest() and test.stopTest() enclose the code.

All triggered were disable and issue was narrowed down to the email alert workflows. At this point is seems to only be some email alerts and not all. I have not been able to narrow down what is different between the ones that cause the issue and the ones that do not.

Is this new or something that is known?

Code:

Account a = New Account();
    test.startTest();
        //Set Mock
        //Make Callout
     test.stopTest();

This causes the uncommitted work pending error.

Code above is about it. I added a field to the Account to check when creating records from test methods Account a = New Account(DoNotSendEmailWorkflow = true);and set the workflow rule to evaluate that and if checked do not do workflow.

When I do this test works just fine.

So, a workflow rule that sends an email alert will cause test methods using mock implementations to fail due to uncommitted work pending.

Update 8-26-2016

This issue is still happening as I just ran into it again.

Update 10-25-2016

now scheduled to be resolved in Winter 17 patch 12

https://success.salesforce.com/issues_view?id=a1p300000008XHBAA2

UPDATE 11-04-2016

SF has resolved this issue. I have not tested it out yet but they now say it was fixed in Winter '17 Patch 12.0

https://success.salesforce.com/issues_view?id=a1p300000008XHBAA2

  • Still happening. Anyone? – Eric Apr 26 '15 at 2:58
  • so -- you insert a test record, that triggers a workflow w/ email alert, then you test.starttest - setMock() -testcode - test.stoptest() ? Might be clearer with a reduced set of your code – cropredy May 14 '15 at 2:10
  • @crop1645 - See updated question. Yes basically it. Disable the rule, tests pass, enable the rule, tests fail. I narrowed it down by disabling ALL trigger and workflow rules and running test. Enable a single rule, run test, rinse and repeat. When I enabled the workflow rule that all it does is send an email alert, test failed with uncommitted work pending error. – Eric May 15 '15 at 15:29
  • 1
    note this known issue : success.salesforce.com/issues_view?id=a1p300000008XHBAA2 and somewhat related: cropredysfdc.com/2014/06/18/… – cropredy May 15 '15 at 18:40
  • @crop1645 - Thanks for finding the first link, I did not see that when I originally posted the issue (not sure if it was reported yet). The second link is well know. Been over a year now IIRC but i do not think it is related at all (or maybe it it) who knows....Either way, can you post the first link as an answer so I can close this question. I do not think there is anything else to be gained here as it is confirmed.... – Eric May 16 '15 at 0:06
5

This may be a known issue -- recently reported and contemporaneous with your OP

  • Resolved as of Winter '17 Patch 12.0 – Eric Nov 4 '16 at 17:43
-2

I arrived here searching for answers to the same problem. You should contact Salesforce support and spend your text 4 weeks playing phone tag with them and trying to get escalated so they can become aware of this and fix the bug ;) I would, but I'm tired of that as I'm doing that with a few other issues right now and I feel like we're beta testers.

Anyways, one workaround I found is to wrap the email call in if (!Test.isRunningTest()) {...} so you can skip it during your test.

Hope that helps!

  • 2
    Skipping the test is NOT a good practice and serves no purpose. I ended up adding a checkbox to the objects and set it to true when the test is updating records. Then on the workflow rule that has email alerts, I set it to evaluate to false if the box is checked. That way my test can still run and fulfill their intended purpose of test our code...Oh, and I have no desire to contact support, it would be futile..... – Eric May 15 '15 at 15:24
  • I'm not saying to skip testing completely, for now I'm just saying to skip the email part as a temporary workaround. – jon_wu May 15 '15 at 19:00
  • You can;t skip the email part (Its a workflow email alert) unless you do what I outlined – Eric May 16 '15 at 0:02
  • Ah yes sorry about that, I read your description too quickly. I was having the exact same problem but Apex was sending the email. – jon_wu May 16 '15 at 16:51
  • This isn't even a valid answer so I'm downvoting it. As the issue reported here is workflow related. – thegogz Apr 27 '16 at 9:47
-3

As per your flow discussed:

1. Insert Account
2. Call Test.startTest()
3. Set your mock
4. Make callout
5. Call Test.stopTest()

I solved this problem already by making @isTest (seeAllData=true) and queried the required Account record for my Callout purposes, the DML pending exception wasn't coming thereafter.

If the above doesn't work for you - (still giving pending DML exception / uncommitted work pending). You can try doing the Account insert inside of System.runas block.

  • 2
    @isTest (seeAllData=true) is not a good practice for writing tests. – Amit Jain Aug 24 '16 at 17:03

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.