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
wiseguywiseguy 

UNABLE_TO_LOCK_ROW while inserting account recrod in test class

I have the following issue:

 

While deploying to production, I am encountering multiple test class failures, reproting the UNABLE_TO_LOCK_ROW error.

The failure is when the test class is trying to create an account record for testing - the error is thrown when trying to insert an account record.

 

When running all tests in a sandbox, the issue does not occur. I was able to test the deployment to a full sandbox with no issue, it's just the production environemnt that is failing.

 

I read the FAQs and trying switching off all autonumber and external id fields, but that did not make any difference. I don;t have any roll up summery fields, I'm not using any multi-threading, so can;t really find any other issue related to an 'insert' statment

 

I tried SFDC support, which refered me to the FAQ (that's premier support money well spent)

 

I'll attach an exrept of the log file, the class cuasing the issue and the account metadata file

'd appraciate any idea as we are currently stuck with this deployment ...

wiseguywiseguy

Error log (I am using a bamboo script, deploying from a bitbucket repo):

 

BUILD FAILED
/var/lib/bamboo/xml-data/build-dir/HM-HM-JOB1/ci_tool/ant/deploy_code.xml:17: FAILURES:
Test failure, method: TestAccountDataCreationUtility.testCreateAndInsertAccountForLastEffectiveCallDateTests -- System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] stack Class.AccountDataCreationUtility.createAndInsertAccountForLastEffectiveCallDateTests: line 34, column 1
Class.TestAccountDataCreationUtility.testCreateAndInsertAccountForLastEffectiveCallDateTests: line 36, column 1

Test failure, method: TestAccountDataCreationUtility.testCreateAndInsertAccountForTests -- System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] stack Class.AccountDataCreationUtility.createAndInsertAccountForTests: line 8, column 1
Class.TestAccountDataCreationUtility.testCreateAndInsertAccountForTests: line 6, column 1

Test failure, method: TestAccountDataCreationUtility.testCreateAndInsertAccountsForTests -- System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] stack Class.AccountDataCreationUtility.createAndInsertAccountsForTests: line 23, column 1
Class.TestAccountDataCreationUtility.testCreateAndInsertAccountsForTests: line 18, column 1

Test failure, method: TestAccountTrigger.testInvalidBillingCountryThrowsErrorOnUpdate -- System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] stack Class.AccountDataCreationUtility.createAndInsertAccountForTests: line 8, column 1
Class.TestAccountTrigger.testInvalidBillingCountryThrowsErrorOnUpdate: line 15, column 1

Test failure, method: TestAccountTrigger.testValidBillingCountryDoesntThrowErrorOnUpdate -- System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] stack Class.AccountDataCreationUtility.createAndInsertAccountForTests: line 8, column 1
Class.TestAccountTrigger.testValidBillingCountryDoesntThrowErrorOnUpdate: line 25, column 1

wiseguywiseguy

This is the test class causing the errors (it works in production - it only fails once the new payload is deployed):

 

@isTest

public with sharing class AccountDataCreationUtility {

    

    public static Account createAndInsertAccountForTests() {

        

        Account account = createAccount();

        

        insert account;

        

        return account;

    }

    

    public static List<Account> createAndInsertAccountsForTests(Integer recordCount) {

        

        List<Account> accounts = new List<Account>();

        

        for (integer i = 0; i < recordCount; i++) {

            Account account = createAccount();

            account.Name += string.valueOf(i);

            accounts.add(account);

        }

        

        insert accounts;

        

        return accounts;

        

    }

    

    public static Account createAndInsertAccountForLastEffectiveCallDateTests(Date LastEffectiveCallDate) {

        

        Account account = createAccount();

        account.Last_Effective_Call_Date__c = LastEffectiveCallDate;

        

        insert account;

        

        return account;

    }

    

    public static Account createAccount() {

        

        Country__c country = CountryDataCreationUtility.createAndInsertCountryForTests();

        

        Account account = new Account (

            Name = 'Default Test Account' + DateTime.now().getTime() + country.Id,

            ShippingCountry = country.Name,

            Phone = '+442071833893'

        );

        

        return account;

        

    }

    

    public static Account createAccountValidFieldEntries() {

    

    Country__c country = CountryDataCreationUtility.createAndInsertCountryForTests();

    

    Account account = new Account (

      Name = 'Default Test Account ' + country.Id,

      ShippingCountry = country.Name,

      Phone = '+442071833893'

    );

    

    return account;

    

  }

 

}

Marko LamotMarko Lamot

post also new payload  class & it's test class

wiseguywiseguy

Hi Marko - the class posted is the new class :-)

 

the new payload contains 647 oblects, however the failure is for the 'createAccount' test class method, consistently

 

I can't paste the account obect xml itself as it is too big ...

Andrew B. DavisAndrew B. Davis
The problem is that when you deploy it's running all of the test classes in parallel, and there is a conflict between your test classes in that two of them are attempting to modify the same Salesforce record at the same time, so one or more of them is seeing that record as locked. There are two possible resolutions. The main resolution is to look at the testing logs to see where the error was being generated and then look at your code to understand why that action was causing your code to attempt to edit a record that was being edited by another test class. Once you resolve that issue your test classes should run fine.

To temporarily circumvent this issue, on Apex Test Execution page click options and tick Disable Parallel Apex Testing. Then the testing will take longer but you won't get the error.