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
Erin Ryan 52Erin Ryan 52 

Hard Delete -UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []

I'm trying to bulk hard delete Account detail childs records but I'm receiving the "UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []" error message. I'm curious if the apex code logic is causing this issue.  Thank you for your time. 

public with sharing class TruceCreateEntityRequest {
    @AuraEnabled
    public static Account getAccount(Id accountId) {
        return [SELECT Name, BillingCountryCode, BillingState, BillingCity, Entity_ID__c, Won_Opportunity_Count__c FROM Account WHERE Id = :accountId];
    }
    @AuraEnabled
    public static String truceCreateEntityRequest(map<string, string> body) {
        // timestamp creation
        String salt = '4947cafc63236413eeaef625cb840679';
        Long longtime = datetime.now().getTime()/1000;
        String timestamp = String.valueOf(longtime);
        // token creation
        Blob blobstamp = Blob.valueOf(timestamp);
        String EncodedTime = EncodingUtil.base64Encode(blobstamp);
        String timerequest = salt + EncodedTime;
        Blob blobRequest = Blob.valueOf(timerequest);
        Blob md5 = Crypto.generateDigest('MD5', blobRequest);
        String token = EncodingUtil.convertToHex(md5);

        String devUrl = 'https://integrations-dev.trucesoftware.com/salesforce/organization.php';
        String prodUrl = 'https://integrations.trucesoftware.com/salesforce/organization.php';

        String sandboxQuery = 'SELECT Id, IsSandbox, Name FROM Organization LIMIT 1';
        List<sObject> databaseResults = Database.query(sandboxQuery);

        String url = prodUrl;
        if ((Boolean)databaseResults[0].get('IsSandbox')) {
            url = devUrl;
        }

        body.put('token', token);
        body.put('timestamp', timestamp);
        String request_body = JSON.serialize(body);
        String companyVal = String.valueOf(body.get('company'));
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(url);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=utf-8');
        request.setBody(request_body);
        HttpResponse response = new Http().send(request);
        String StrResponse = string.valueOf(response);

        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());

            // Grab entity ID from response body
            String res = response.getBody();
            String[] org = res.split(':');
            String orgID = org[2];
            orgID = orgID.substring(0,orgID.length()-1);
            // Update company by assigning it its entityID from Database
            try {
                List<Account> accountUpdate = [SELECT Entity_ID__c FROM Account WHERE Name = :companyVal LIMIT 1];
                Account entity = accountUpdate[0];
                entity.Entity_ID__c = orgID;
                update entity;
            } catch(Exception e) {
                System.debug('An unexpected error has occurred: ' + e.getMessage());
            }
        }
        return response.getBody();
    }
}
Best Answer chosen by Erin Ryan 52
SwethaSwetha (Salesforce Developers) 
HI Erin,
Looks like an issue because of data Skewing.

I came across this post that should help https://salesforce.stackexchange.com/questions/20921/can-anybody-explain-the-unable-to-lock-row-error

https://www.infallibletechie.com/2020/04/unabletolockrow-unable-to-obtain.html

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Erin,
Looks like an issue because of data Skewing.

I came across this post that should help https://salesforce.stackexchange.com/questions/20921/can-anybody-explain-the-unable-to-lock-row-error

https://www.infallibletechie.com/2020/04/unabletolockrow-unable-to-obtain.html

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Erin Ryan 52Erin Ryan 52
Thank you! I'll look into provided resolution.