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
VidhiVidhi 

Price Book Entry

Hello,

I am trying to insert price book enty in test class.When i have inserted this and run that class it worked fine. But My so may test class is using price book.As i ran all the test class by clicking "Run All Test"  It is giving error

"System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []"
How canm i solve this granulatity issue.

My code is this

Id pricebookId = Test.getStandardPricebookId();
     
        PricebookEntry standardPrice = new PricebookEntry(
             Pricebook2Id = pricebookId, Product2Id = objProduct.Id,
             UnitPrice = 10000, IsActive = true);
        insert standardPrice;
       
        // Create a custom price book
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
       
        // 2. Insert a price book entry with a custom price.
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = objProduct.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
Shabbir ShaikShabbir Shaik
Hi Vidhi,

Try like this,
@isTest(seeAllData=true)

If this is helps you, close the question by choosing a best answer.
Pramod_SFDCPramod_SFDC
Hi,

In general, these errors occurs, when you try to perform more than one operation on a record in the same transactions.Also as per your coomments, you informed that you are having many test class is using price book. Hence, it clear tells that there is chance of Record getting locked.

Workaround could be like, try to use test data in you testclasses rather than using the real data, which is considered to be best practices. Else, you should check this by using debug logs and verify, which recoord is getting locked and change your code accordingly or use @future method in your test class.

Regards
Pramod
Brian RichardsonBrian Richardson

Using @SeeAllData=true is not a legitimate workaround for this issue. All test data should be generated in the test method or a shared test class, rather than pulled from a production org. What happens when you run all tests in a refreshed develper sandbox with @SeeAllData=true? Any test relying on data will fail!

I have this same problem in my production org, generating PricebookEntries as follows:

 

Id pricebookId = Test.getStandardPricebookId();

List<PricebookEntry> entries = new List<PricebookEntry>();
PricebookEntry sp1 = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod1.Id, UnitPrice = 10000, IsActive = true);
entries.add(sp1);

PricebookEntry sp2 = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod2.Id, UnitPrice = 20000, IsActive = true);
entries.add(sp2);

PricebookEntry sp3 = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod3.Id, UnitPrice = 30000, IsActive = true);
entries.add(sp3);

PricebookEntry sp4 = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod4.Id, UnitPrice = 40000, IsActive = true);
entries.add(sp4);

PricebookEntry sp5 = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod5.Id, UnitPrice = 50000, IsActive = true);
entries.add(sp5);

PricebookEntry sp6 = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod6.Id, UnitPrice = 60000, IsActive = true);
entries.add(sp6);

insert entries;

Going to try to do individual inserts on these, and will report back if the problem persists.
Emil Mr No regression DoverlindEmil Mr No regression Doverlind
Turn off parallel testing, if the tests is running in serial the problem with locking the standard pricebook will disappear.

See http://salesforce.stackexchange.com/questions/143093/avoid-exclusive-access-to-the-standard-pricebook/143099