Skip to main content
All CollectionsTroubleshootingCI Jobs and org monitoring
Troubleshooting error "UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record" in Unit test jobs
Troubleshooting error "UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record" in Unit test jobs

Best practices to avoid UNABLE_TO_LOCK_ROW error in Salesforce Apex testing with Gearset

Jacob Joshua avatar
Written by Jacob Joshua
Updated over a week ago

Overview

This document outlines the steps to prevent the "UNABLE_TO_LOCK_ROW" error during automated testing in Salesforce when run via Gearset. The error arises due to row locking conflicts caused by concurrent access to records, typically during parallel execution of Apex tests.

Error description

The error message seen in the Salesforce logs:

System.DmlException: Update failed. First exception on row 0 with id XXXXXXXXXXXXXX; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive

access to this record:

This error can happen if the tests are manually run through the Salesforce UI or via the Tooling API, where the tests are run in parallel. Because Gearset test monitors use the Tooling API, this means your test monitors will run all of your tests in parallel (asynchronously).

One potential solution is to disable the Parallel Apex testing for Apex test execution as mentioned in this article.

Other processes (triggers, flows, batch jobs) might also access the same records during test execution, contributing to the row locking issue. Ensure that the test methods do not overlap in their use of shared data as well, which may cause row locking.

Conclusion

The "UNABLE_TO_LOCK_ROW" error primarily occurs due to simultaneous record access during test runs. By configuring Salesforce to run tests serially, staggering test jobs, and optimizing test data and automation, you can significantly reduce the occurrence of this error.

Did this answer your question?