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
Pooja Singh 21Pooja Singh 21 

System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record

Hi All,

I am getting this error while performing 'Run All Tests'. However, individual test classes works fine. This error is also intermittent. I am not sure how to fix this and why I'm getting this error. Can anyone help?

magicforce9magicforce9
Hi Pooja,

I can see probably where the problem might be as you said the Tests run fine when you run them inidividually. I'm assuming you got (SeeAllData=true) set for those test classes...if so - then I highly suggest to re-think if you really need visibility to the existing data....

Another alternative might be to use a Static variable in an Independed class and where you see there is a DML statement resulting in this error..set/change the value of static variable...On a side note - do remember that when you do DML on a sObject salesforce also locks the Parent Object relating to that record if there are any in Master-Detail relationship.

Hope it helps..

Thanks,
Mohammed
 
siddarth rajsiddarth raj

Hello Pooja,

Enable the property 'Disable Parallel Apex Testing'. This you can do by following

Setup->Develop->Apex Test execution ->Click on options button a window will be shown enable the property mentioned, and try running all tests again.

This will for sure sort your problem.

Thanks,
Sid

Pooja Singh 21Pooja Singh 21
Thanks Mohammed and Siddarth for your reply.

Running all the tests clsses sequentially helps me removing the error. However, I am facing a new issue when I deploy my change set. I get 'System.LimitException: Apex CPU time limit exceeded' error for all the methods of test classes. I am not sure how to deal with it.
Pooja Singh 21Pooja Singh 21
Can anyone help me with the above issue?
Surgaya KhundrakpamSurgaya Khundrakpam
Hi Pooja,

Check if there are any update statements inside a for loop. If so then remove and try.

You may find of the best practice to write apex code: https://developer.salesforce.com/page/Apex_Code_Best_Practices