[SalesForce] Records are not getting locked with “Approval.LockResult” class

On Using "Approval.LockResult" class, In debug logs i can see the records are successfully locked but in reality the records are not saved and i cannot even see the lock/unlock button on record detail page.

Below is the code:

// Query the accounts to lock
Account[] accts = [SELECT Id from Account WHERE Name LIKE 'Ab%'];
// Lock the accounts
Approval.LockResult[] lrList = Approval.lock(accts, false);

// Iterate through each returned result
for(Approval.LockResult lr : lrList) {
    if (lr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully locked account with ID: ' + lr.getId());
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : lr.getErrors()) {
            System.debug('The following error has occurred.');                    
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}

NOTE:
1. User have ModifyAll access to Object and also have ModifyAllData system permission.
2. As part of prerequisite, i have also enabled the 'record locking and unlocking in Apex' process automation setting.

Best Answer

This is the default behavior. System Admins (i.e. users with permission of Modify All/Modify All Data) can edit the locked records. Here is the link which states the same. You can go through this article which mentions what permissions are needed to edit the locked records.

If you are not seeing the lock/unlock button in the page layout, you need to check following points.

  • Lock/Unlock button will be visible only for users with Modify All/Modify All Data permission
  • Disable "Enable Separate Loading of Related Lists" setting under user interface

And if the user has Modify All/Modify All Data permission or is assigned as currently assigned approver, then that user will be able to edit the record without having to click on Unlock button. Lock/Unlock button is just to lock/unlock the record. System Admins (Modify All/Modify All Data) can directly click on edit button and edit the record.

Related Topic