function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShikibuShikibu 

UNABLE_TO_LOCK_ROW on insert (unit test)

I have a unit test that is throwing UNABLE_TO_LOCK_ROW when it is doing an insert. I don't understand why that should happen. Here's the code (excerpted). The exception is thrown on the insert statement. Because this is a new record, and contained within a test class, how can it throw a UNABLE_TO_LOCK_ROW exception?

@istest
private class MyTest {
    ...
    theQuote = new Quote(Name='Test Quote',
                         OpportunityId = theOpp.Id,
                         Term__c=12,
                         pricebook2id=thePricebook.id);
    insert theQuote;
    ...
}


kevin Carotherskevin Carothers
is it possible you have conflicting or recursive triggers?  Possibly on the Opportunity?
FWIW, Here's a link to an article about preventing recursive triggers;

http://appirio.com/category/tech-blog/2009/10/preventing-recursive-future-method-calls-in-salesforce/


ShikibuShikibu
Here's some more context. The test class works fine if executed by itself, but fails when you do "run all tests" or try to deploy new code. It is failing when there is parallel execution of test classes. It's old code, which has worked fine for a long time, and has not been changed recently.
kevin Carotherskevin Carothers
Hmmm- I'd look to see what other triggers are being run during the test... Maybe there's other triggers or classes that are changing your pricebook (if it's the default pricebook - which it probably is).  I'm just guessing, but maybe if you put a "FOR UPDATE" on the SOQL that selects thePricebook in your trigger it might help.

There's still some missing context -- if you create a "Test Pricebook" on both sandbox and production, you might be able to get away with using that pricebook in your test for this trigger, but I'd still look at other trigger code.

Things obviously get messy if you have managed packages that are running tests that have triggers that you can't access the code to...

Mohammad Asim AliMohammad Asim Ali
Disable parallel testing