How to implement edit locking

When 2 users start editing the same record, the first user who saves the record wins, and the other user will lose the changes with an error message similar to “The record you were editing was modified by *someone* during your edit session. Please re-display the record before editing again.”

This can be an inconvenience:  if an integration process updates the records, any user who opened the record for editing will lose his changes if he/she doesn’t hit Save quick enough.

To lock the record when the first user opens it for editing, you can implement the following 4 steps:

  1. Create a date/time field “Last Edit Date Time” – this field will store the date and time when the record was opened for editing.
  2. Create a validation rule to prevent any updates to the record during the period when it is locked (a period of 5 minutes so that no one can keep the record locked indefinitely).
  3. Create a Javascript “Edit” button that updates the “Last Edit Date Time” field and redirects to the record editing page.
  4. Replace the standard Edit button on the page layout with the custom Edit button.

First we create the field (this example uses Accounts):

Screen Shot 2014-12-30 at 6.29.17 PM

Then we create the validation rule as follows:

AND( NOW() – PRIORVALUE( Last_Edit_Date_Time__c ) < ( 5/1440 )
/* locks for 5 minutes */
, PRIORVALUE( LastModifiedById ) != $User.Id )

The first line in this validation rule calculates how long since the last time the record was locked and compares the number to 5/1440 (5 minutes divided by the number of minutes in a day).

If it is less than 5 minutes, then, on the second line, it checks whether the user doing the update is the same user that set the edit lock.

If it is the same user, it will allow the update to go through, but if it is a different user, it shows an error message. I chose the error message to be “The record is locked for editing by another user (5 minutes duration).”

Lastly, we need to create a new Button on Account to run Javascript code that updates the Last_Edit_Date_Time__c and redirects to the the account standard editing page:

Screen Shot 2014-12-30 at 7.20.18 PM

This is the Javascript code:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

// prepare account for updating the last edit date time
var anAccount = new sforce.SObject( "Account" );
anAccount.id = '{!Account.Id}';
var dtNow = new Date();
var nowInJSON = dtNow.toJSON();
anAccount.Last_Edit_Date_Time__c = nowInJSON;

// update the account
var result = sforce.connection.update( [ anAccount ] );

window.location.href = '/{!Account.Id}/e';

Now we need to just remove the standard Edit button and place the new custom Edit button on the page layout.

Screen Shot 2014-12-30 at 7.25.41 PM

This makes it so that if an user clicks Edit, it will set the Last Edit Date Time. The validation rule will prevent updates to the record from anyone else during the next 5 minutes. If another user attempts to save the same record, the validation rule will cause this error message to appear:

Screen Shot 2014-12-30 at 6.02.45 PM

 

 

Advertisements

Posted on 12/31/2014, in No category, validation. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: