Lock Records In Salesforce

How To Lock Records In Salesforce | The Complete Guide

The client shouldn't have the option to edit the field values of the record. Salesforce will try up to 10 times to obtain a record lock, before giving up, and throwing an error. Salesforce first locks the record so nobody else can refresh it until the lock is delivered.

At the point when a sObject record is locked, no other customer or client is permitted to make refreshes either through code or the Salesforce UI. the locked records won't be changed by another customer during the lock time frame.

Advertisement

There are 7 ways to lock a record using UI and coding. There is no "best" approach among these 7 ways, each approach has its own advantages and disadvantages. The methodology you pick simply relies upon what suits your necessity. 

Here Are The Following Ways to Lock Your Records

  1. Apex – Lock() and Unlock()
  2. Validation Rule
  3. Using Record Ownership
  4. Approval Process

1. Apex – Lock() and Unlock()

New Lock() and Unlock() strategies in the System. Approval namespace, lock records by going in record IDs or sObjects. Beforehand, you could set endorsement measure lock and Unlock just through the Salesforce UI

dont miss out iconTo empower this element, from Setup, then enter Process Automation Settings in the Quick Find box, at that point click Process Automation Settings. Then select Enable record securing and opening Apex. 

Don't forget to check out: Get Selected Records from ListView in Visualforce Page | GETRECORDIDS JavaScript | StandardSetController Salesforce

Here are examples:

Account[] acc = [SELECT Id from Account WHERE Name LIKE 'Acme%'];
Approval.LockResult[] DemoList = Approval.lock(acc, false);
for(Approval.LockResult lr : DemoList ) {
    if (lr.isSuccess()) {
    } 
}
else {
    // Operation failed, so get all errors   message              
    for(Database.Error err : lr.getErrors()) {
        System.debug('The following error has occurred.');                    
        System.debug(err.getStatusCode() + ': ' + err.getMessage());
    }
}

2. Validation Rule

By using functions PRIORVALUE() and ISCHANGED() to detect any changes happening in  record. client will get an error when saving a record. Block the entire record or data, we need to add all the ISCHANGED conditions for all the fields in the validation rule.

In this way, even System Administrator won’t be able to edit the record, able to indicate for which Profiles this validation run the show apply by specifying the Profile Title within the approval Run the show.

The drawback of this method is that the mistake message appears as it was when the client clicks on Save. 

3. Using Record Ownership

Alter the proprietor of the record utilizing workflow field upgrade or trigger and make beyond any doubt that the OWD (Association Wide Default) and sharing rules are set up accurately to anticipate upgrades or erases. Once more this may not be an alternative for you in case you can’t alter the record proprietors because it may have other impacts (e.g. reporting)

dont miss out iconCheck out another amazing blog by Ayush here: Platform Events in Salesforce - An Introduction

4. Approval Process

Another method to lock a record is utilizing the Approval Process. The locking includes maybe a standard highlight given by Salesforce and the whole record will be locked. Well, the complete reason for the Approval Process isn’t around locking, it is almost how records are affirmed in Salesforce. An approval process specifies each step of endorsement, counting who to ask endorsement from and what to do at each point of the method. We are fair utilizing an Activity from the Approval Process.

Reference: sfdcfanboy, sfdctechie.wordpress, trailhead.salesforce

Responses

Popular Salesforce Blogs