0

I have been building more code that touch products and as such am referring to the standard pricebook with my test classes (I create products and pricebook entries related to the standard pricebook).

What I'm finding is that if I run my test classes in parallel sometimes they would fail with:

UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record

However, if I run them separately they pass as expected. is there a way to refer to the standard pricebook id without locking the record for the other tests? I have 3 tests that reference the pricebook with Test.getStandardPricebookId() but they keep tripping over each other.

  • First of all, without seeing your code is hard to help you. Second of all, consider replacing insert with upsert. Lastly, you can inject some randomness in record names with Math.random. – Adrian Larson Oct 4 '16 at 20:12
  • Smells like an X/Y problem. If this is with test code, I am not sure why is there a problem. Tests run serially when deploying, so they won't fail when moving to Production. – Sebastian Kessel Oct 4 '16 at 20:12
  • It was when I was deploying to production so the deployment failed, I ran all tests from the APEX Test Execution UI, it failed. I went to lunch tried it all again it passed. Tried the individual classes that failed they passed. It's strange to me. – Dan Wooding Oct 4 '16 at 20:35
  • Unfortunately, not very. DS's answer below will clarify. – Sebastian Kessel Oct 4 '16 at 21:01
3

Unfortunately, this is not really possible to work around. The best you can do today is place all tests referencing the Pricebook in a single class since tests in a class always run in serial (even if parallel testing is enabled).

Our tests heavily use Pricebook2, so we have never been able to turn on parallel testing to speed up our test runs.

| improve this answer | |
  • N.B. for an org with custom apex around OLI, quoting, orders, assets, and/or service contracts - all of which use PricebookEntry, one test class is not very practical. My experience is same as yours. – cropredy Oct 4 '16 at 22:35
  • This is the number one pain point for our devs moving to SFDC from a more modern dev environment: who wants to wait 2+ hours for all tests to run on their pull request before you are sure that you have not introduced any regression bugs? I have not checked out the new Salesforce DX yet but hopefully this is addressed! – dsharrison Oct 5 '16 at 1:37

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.