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 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
  • Now select a record whose details you want to edit .
  • record
  • 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
  • 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 ob= new  List([select name,phone,fax, id from account ]);
        //initialising Method.
        public list 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([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 ob=new  List();
                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;
        }
    }
    

    About Girikon

    Girikon is a Salesforce consulting company,development team are based in the USA, in Noida, India and offices in Australia. Girikon’s global team in the USA, India and Australia, allows Girikon to respond at Lightning speed to customers across the globe and is known for its effective delivering and quality service. Girikon is made up of a team of certified Salesforce Consultants with experienced Project Managers, Business Analysts, Salesforce Architects, IT Developers, Consultants, and Administrators.

    Girikon’s team of dynamic professionals are experienced in IT across many industries and business, their specialities include software development which includes design, QA testing (Manual and Automated, Support and Maintenance and have many resource model options. Our vision is to develop scalable and simplified solutions for our customers.

    About Author
    Gaurav Kapoor
    Gaurav Kapoor is currently a Salesforce Developer at Girikon working for the company for nearly Two months. Skilled in Salesforce.com, Java, JQuery, SQL, Creative Style Sheets (CSS), HTML graduated from Rawal Institute of engineering and management, Faridabad.
    Share this post on: