0

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

2
  • Can you share your code and the line numbers where you are getting the error? Jul 14, 2014 at 13:10
  • Error Message System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] Stack Trace Class.UnhandledExceptionEmailService_TEST.severityLevelValue: line 51, column 1 Class.UnhandledExceptionEmailService_TEST.unhandledException: line 64, column 1
    – suresh
    Jul 14, 2014 at 13:20

1 Answer 1

3

You get this error message when the record that you are updating is locked. To avoid this issue in a test class you should create your own test data inside your test class instead of relying on the data in the org. This is a best practice to avoid test failures in different sandboxes. Create or setup your data between Test.start() and Test.stop() before doing any validation or assertions.

Alternatively, there might be a batch job or data loader job updating records that you are trying to do dml on. Also a possibility but less like is lock contention due to account data skew.

https://developer.salesforce.com/blogs/engineering/2013/01/reducing-lock-contention-by-avoiding-account-data-skews.html

hope this helps!

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