Loading

Error 'UNABLE_TO_LOCK_ROW' when running test classes

Publiseringsdato: May 23, 2026
Beskrivelse

The UNABLE_TO_LOCK_ROW error occurs when two or more database operations attempt to modify the same record simultaneously and one transaction cannot obtain an exclusive lock on that record. In Salesforce, this commonly occurs when running Apex test classes that insert community or portal users while parallel test execution is enabled. When multiple test threads try to insert or modify the same user or related records at the same time, a row-lock conflict is triggered.

Løsning

Working as Designed

This behavior is working as designed. Salesforce enforces row-level locking to maintain data integrity during concurrent transactions.

Workarounds

Apply one or more of the following workarounds to prevent this error:

  1. Use the RunAs() method in test classes instead of creating new community or portal users. RunAs() temporarily switches the execution context to a specific user without inserting new user records, avoiding the lock.
  2. Run deployments during off-peak hours to reduce the chance of concurrent transactions on shared records.
  3. Temporarily disable any scheduled jobs that create portal or community users before starting test class validation.
  4. Disable parallel test execution for test classes that insert portal or community users. In the Salesforce Developer Console or VS Code, configure test runs to execute serially.

Need Further Help?

If the error persists after applying these workarounds, contact Salesforce Support for additional assistance.

Knowledge-artikkelnummer

000380602

 
Laster
Salesforce Help | Article