Thursday, April 30, 2015

Avoid simultaneous update/ Control same record if already opened by someone in edit mode & show a message to end user

Recently I came across a requirement where client wanted to track if any other user also open the same record to edit, in this case he wanted to show a message to second user so he can edit the same record latter.

To solve this I created a custom setting let’s say Control_Record_Edit__c. Created few other fields to track edit mode –

Name
Edit_DateTime__c
User_Id__c

Name:- field will save record id which is opened in edit mode by someone.
Edit_DateTime__c:- field will tell us how long someone open a record in edit mode
User_Id__c:- field will tell us who open that record in edit mode.


Now let’s say we want to track lead record. We will create a new VF page that will be called LeadEdit.page. In this page we will have a function called init which will basically insert a record in our custom setting, so next time whenever someone open the same lead it edit mode we can check in code & show a message that another user is already opened this lead in edit mode.

<apex:page standardController="lead" extensions="LeadEditController" action="{!init}">
 <apex:form id="form1">
  <apex:outputPanel rendered="{!isNeedToShowMsg}">
   <apex:pageMessages id="error"/>
   <apex:commandButton value="Go To Lead" action="/{!objLead.Id}" style="margin-left: 20px;"/>
  </apex:outputPanel>
 </apex:form>  
</apex:page>


Class code is as below
public with sharing class LeadEditController {
 public Lead objLead {get;set;} 
 public boolean isNeedToShowMsg {get;set;}
 public LeadEditController(ApexPages.StandardController stdController){
  objLead = (Lead)stdController.getRecord();
 }
  
 public PageReference init(){
  isNeedToShowMsg = false;
  PageReference pr = NULL;
  List listOfControlLeadUpdate = [SELECT Id,User_Id__c,Edit_DateTime__c from Control_Lead_Update__c where Name=:objLead.Id and User_Id__c !=:Userinfo.getUserId() LIMIT 1];
  if(listOfControlLeadUpdate != NULL && !listOfControlLeadUpdate.isEmpty()){
   List listOfUser = [Select Id, Name from User where Id=:listOfControlLeadUpdate.get(0).User_Id__c LIMIT 1];
   if(listOfUser != NULL && !listOfUser.isEmpty()){
    String Msg = 'Another User ' + listOfUser.get(0).Name + ' already start editing the same lead at '+  listOfControlLeadUpdate.get(0).Edit_DateTime__c.format()  +', Please wait for sometime & click on edit button again!'; 
    ApexPages.Message apexmsg = new ApexPages.Message(ApexPages.Severity.INFO, Msg);
             ApexPages.addMessage(apexmsg);
             isNeedToShowMsg = true;
   } 
   
  }else{
   Control_Lead_Update__c objControlLead = new Control_Lead_Update__c();
   objControlLead.Name = objLead.Id;
   objControlLead.Edit_DateTime__c = datetime.now();
   objControlLead.User_Id__c = Userinfo.getUserId();
   try{
    insert objControlLead;
   }catch(Exception ex){
    system.debug('exception::-->>>' + ex);
   }
   pr = new PageReference('/' + objLead.Id + '/e?nooverride=1&retURL=/apex/LeadCancel?Id=' + objLead.Id);
  } 
  return pr; 
 }
 
 public PageReference initCancel(){
  LeadHandler.ReleaseEditModeForOthers(new List{objLead});
  PageReference pr = new PageReference('/' + objLead.Id);
  return pr;
 }
}



Here after this one other issue appear when user click on cancel, if user open a lead & click on cancel we should remove his record from custom setting but the problem is cancel override is not available in salesforce. To handle this we created another page that is called LeadCancel & we are passing it in the retURL parameter.

<apex:page standardController="lead" extensions="LeadEditController" action="{!initCancel}">

</apex:page>

now in the class code there is a method initCancel, that is basically deleting the record from custom setting if a user open a lead & click on cancel button. also we are passing this cancel page in retuURL parameter so when user click on cancel it will be back to our page.

There is only one point here which is important, if a user open a lead & close it by browser close button then we can not track that action & in that case no one can edit the same lead until that record does not removed from custom setting.

No comments:

Post a Comment