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, 2016 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. Oct 4, 2016 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. Oct 4, 2016 at 20:35
  • Unfortunately, not very. DS's answer below will clarify. Oct 4, 2016 at 21:01

2 Answers 2


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.

  • 1
    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, 2016 at 22:35
  • 1
    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, 2016 at 1:37

Disable Parallel Apex Testing (Develop - Apex Test Execution - Options)

  • This answer could use some details, but I do think it's possible that parallel Apex tests can lead to a row lock if the same tests grab the same data. @AFC could you edit your answer to provide some more details as to why disabling parallel testing could avoid this error?
    – nbrown
    Dec 14, 2020 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .