EDITING LOCKING MECHANISM SALESFORCE OBJECT DETAIL
Tags: salesforce, beginning, coding, tutorial
Published: 2019-12-02 12:04:13

---
title:  EDITING LOCKING MECHANISM SALESFORCE OBJECT DETAIL
published: true
date: 2019-04-30 10:08:01 UTC
tags: Salesforce, beginning, coding, tutorial
canonical_url: https://www.girikon.com/blog/salesforce-consultants-assisted-by-the-editing-locking-mechanism-salesforce-object-detail/
---

 **INTRODUCTION**

When working on any object in Salesforce and with records we want to edit we can do it however when another user is also editing the same record at the same time in same Salesforce org then both users will have the previous save details to edit. Here is the detail of the problem, the user who saves the record first will change the details then the other user editing the same record is still editing the previously saved record. This causes issues when the second user doesn’t know the first record has changes and they are just about to change an old record.

As a Salesforce Consultant I have had the QA team reject a piece of functionality due to changes to multiple changes to the same Salesforce object where I am either first, second or even the third user to make the changes to the same object. **[Girikon’s Salesforce Consulting services](https://www.girikon.com.au/salesforce-consultant/)** team is made up of hundreds of Salesforce consultants and as many as 10+ could be working on the same project at one time which sometimes makes it difficult for all consultants to know which objects the other consultants are editing.

**THE SOLUTION IS EASIER THAN YOU THINK…**

Now let’s think about the solution. We could lock the record for other users. This is a great feature so that second and third users must refresh in order to get the new details.

Whenever two or more users open the Salesforce object record to edit then the user who triggers the first save event has priority and will be able to save the record. When the second user saves the changes then user will get locked and the record will not get saved. To remove the lock, the second user will need to refresh and re-open the editing Window for that particular record. Now the user will be presented the new details which were saved by the first user. I have developed a practical guide below to assist with eliminating the problem.

 

**FOLLOW THE STEP BY STEP SOLUTION BELOW…**

- First, the records of the object (that you want to edit) should be displayed on the screen through visual force page or lightning.
[![lightning](https://www.girikon.com/wp-content/uploads/2019/04/lightning.png)](https://www.girikon.com/wp-content/uploads/2019/04/lightning.png)- Now select a record whose details you want to edit .
[![record](https://www.girikon.com/wp-content/uploads/2019/04/record.png)](https://www.girikon.com/wp-content/uploads/2019/04/record.png)- If two or more users are editing the same record at the same time, then the user who will click on the save button on priority will only be able to save the record.
- If other users click on the save button the they will get directed to a new page which will show the message to refresh the page.
[![new page](https://www.girikon.com/wp-content/uploads/2019/04/new-page.png)](https://www.girikon.com/wp-content/uploads/2019/04/new-page.png)- For creating this you must add the below mentioned Visual Force code in your VF page.

```
<apex:page controller="lockingMachenismForAnyObject" sidebar="false"> <apex:form > <apex:pageBlock > <apex:pageBlockSection> <apex:inputText value="{!searchName}" label="ENTER THE NAME TO EDIT"/> </apex:pageBlockSection> <apex:commandButton value="search" action="{!search}"> </apex:commandButton> <apex:pageBlockTable value="{!ReturningList}" var="v"> <apex:column headerValue="USER NAME" title="NAME"> <apex:outputField value="{!v.name}"/> </apex:column <apex:column headerValue="USER PHONE NUMBER" title="PHONE NO"> <apex:outputField value="{!v.phone}"/> </apex:column < apex:column headerValue="USER FAX NUMBER" title="FAX NO"> <apex:outputField value="{!v.fax}"/> < /apex:column> < apex:column headerValue="USER ID" title="ID"> < apex:outputField value="{!v.id}" /> </apex:column> <apex:inlineEditSupport event="ondblClick"> < showOnEdit="saveButton,cancelButton"/> </apex:pageBlockTable> <apex:pageBlockButtons> <apex:commandButton value="Save" action="{!save}" id="saveButton"/> < apex:commandButton value="Cancel" action="{!cancel}" id="cancelButton"/> </apex:pageBlockButtons> < apex:pageBlockSection > <apex:inputText value="{!newName}" label="Enter New NAME"/> </apex:pageBlockSection> <apex:pageBlockSection> <apex:inputText value="{!newPhone}" label="Enter New PHONE"/> </apex:pageBlockSection> <apex:pageBlockSection > < apex:inputText value="{!newFax}" label="Enter New FAX"/> </apex:pageBlockSectio > < /apex:pageBlock> </apex:form>
```
- For the above VF code below one is the apex code.

```
public without sharing class lockingMachenismForAnyObject { //Describing all the variables. public string searchName{get;set;} public string idOfSearchedName{get;set;} public string newName{get;set;} public string newPhone{get;set;} public string newFax{get;set;} // Fetching the list of Account records. public List<account> ob= new List<account>([select name,phone,fax, id from account]);
    //initialising Method.
    public list<account> getReturningList() { 
        return ob;
    }
    //initialising Method.
    public void search()
    {
        //Modifying the above list according to the name of the record which we want to edit.
        ob= new List<account>([select id,name,phone,fax from account where name = : searchName limit 1]);
        idOfSearchedName = ob[0].id;
        Account[] accountObject = [SELECT Id FROM Account where name = : searchName];
        //Checking the locking condition.
        if(Approval.isLocked(accountObject[0].id))
        {
            Approval.unLock(accountObject);
        }
    }
    //initialising Method.
    public pageReference save()
    {
        if(Approval.isLocked(idOfSearchedName))
        {
            //Sending the user to the locked page if the locking condition is satisfied.
            pageReference pr=Name of the connected page to display.
            return pr;               
        }
        else
        {
            //Else saving the record and locking the same record for the other user.
            List<account> ob=new List<account>();
            Account updateObject=[select id,name,phone,fax from account where id = : idOfSearchedName];
            updateObject.name=newName;
            updateObject.phone=newPhone;
            updateObject.fax=newFax;
            ob.add(updateObject);
            update ob;
            Account[] accountObject = [SELECT Id FROM Account where name = : searchName];
            Approval.lock(accountObject);
            return null;
        }
    }
    //initialising Method.
    public pageReference cancel()
    {
        pageReference pr=page.pramodSirVF2;
        return pr;
    }
}
</account></account></account></account></account></account>
```

The post [SALESFORCE CONSULTANTS ASSISTED BY THE EDITING LOCKING MECHANISM SALESFORCE OBJECT DETAIL](https://www.girikon.com/blog/salesforce-consultants-assisted-by-the-editing-locking-mechanism-salesforce-object-detail/) appeared first on [Salesforce Consulting Company | United States | Phoenix, Arizona](https://www.girikon.com/).