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
ministe_2003ministe_2003 

UNABLE_TO_LOCK_ROW in test code since summer 14

Hi all,
Since updating to summer 14 a lot of my tests are failing with the UNABLE_TO_LOCK_ROW error.  It's all coming from the same line of code, which is some global code to generate records for testing many different things, such as sample accounts, opportunities etc.  This is the section that's failing:

@IsTest(SeeAllData=true)
    public static void createTestPricebookEntry(){
    	if(pbeCreated == null){
				pbeCreated = false;
			}
			if(!pbeCreated){
    		pbeCreated = true;
	    	//stdPbId = Test.getStandardPricebookId(); activate once we update to summer 14
				stdPbId = [SELECT Id FROM Pricebook2 WHERE isStandard = true].Id; //deactivate once we update to summer 14
	    	Product2 p2 = new Product2(Name = 'Engineered - Spur', ProductCode = 'EO', isActive = true);
	    	insert p2;
	    	
	    	newProdId = p2.Id;
	    	
	    	PricebookEntry stdPbe = new PricebookEntry(Product2Id = newProdId, Pricebook2Id = stdPbId, unitprice = 50, useStandardPrice = false, isActive = true, CurrencyISOCode = 'GBP');

	    	insert stdPbe;       <-- THIS IS THE FAILING LINE
	    	pbeId = stdPbe.Id;
    	}
    }
This method can be called from many different places but is mostly called from another similar test class which creates a test opportunity with product lines.  It's causing 20 of my classes to fail which worked perfectly until the update to Summer 14.  Does anyone know why this is suddenly failing and what I can do to prevent it?  The class is a public but all my variables and methods are static, which I thought should prevent this problem.

Thanks
Best Answer chosen by ministe_2003
ministe_2003ministe_2003
Yes, you need to click Setup | Develop | Apex Test Execution then click the Options... button.  Tick the box to "Disable Parallel Apex Testing".  Since this problem only started occurring in Summer 14, I assume that prior to that, all testing was done one at a time as standard.

All Answers

Ankit AroraAnkit Arora
Not sure completely, but are you aware of the new enhancement regarding pricebook in test classes?

http://docs.releasenotes.salesforce.com/en-gb/summer14/release-notes/rn_apex_price_books_in_tests.htm
http://peterknolle.com/salesforce-pricing-enhancements/
ministe_2003ministe_2003
I am, and I think that might be part of the issue.  However I don't think that new code applies to me because we only have one pricebook and that is the standard pricebook (is that where I'm going wrong?  Should I have another one which come from that?) and as I understand it, that new code does not allow me to use the standard pricebook without SeeAllData, only non standard ones.
ministe_2003ministe_2003
Actually having written that, I've just replaced the query for the standard pricebook Id with the getStandardPricebookId method and reduced the number of failing classes from 20 to 16, so it's helped a little but not a lot.  You can see from my original post that I had the line in there, commented out, with the intention of activating it once the summer 14 update was finished but I had forgotten to do so.
ministe_2003ministe_2003
Just ran all tests again, this time I got 18 errors so it seems pretty random as to how many fail!
min liumin liu
Hi ministe_2003, I encountered the same problem. Did you have a workaround for this?
ministe_2003ministe_2003
Yes, you need to click Setup | Develop | Apex Test Execution then click the Options... button.  Tick the box to "Disable Parallel Apex Testing".  Since this problem only started occurring in Summer 14, I assume that prior to that, all testing was done one at a time as standard.
This was selected as the best answer
sean*harrisonsean*harrison
Disabling parallel testing is a good workaround. How to make test code safe for this error condition?