{"id":222,"date":"2014-12-31T06:45:07","date_gmt":"2014-12-31T01:45:07","guid":{"rendered":"http:\/\/learnsf.wordpress.com\/?p=222"},"modified":"2014-12-31T06:45:07","modified_gmt":"2014-12-31T01:45:07","slug":"how-to-implement-edit-locking","status":"publish","type":"post","link":"https:\/\/blog.felineflock.com\/index.php\/2014\/12\/31\/how-to-implement-edit-locking\/","title":{"rendered":"How to implement edit locking"},"content":{"rendered":"<p>When 2 users start\u00a0editing the same record, the first user who <strong>saves<\/strong> the record wins, and the other user will lose the changes with an error message similar to <span style=\"color:#ff00ff;\"><strong>&#8220;The record you were editing was modified by *<em>someone*<\/em> during your edit session. Please re-display the record before editing again.&#8221;<\/strong><\/span><\/p>\n<p>This can be an inconvenience: \u00a0if an integration process updates the records, any\u00a0user who opened the record for editing will lose his changes if he\/she doesn&#8217;t hit Save\u00a0quick enough.<\/p>\n<p>To lock the record when the first user opens it\u00a0for editing, you can implement the following 4 steps:<\/p>\n<ol>\n<li>Create a date\/time field &#8220;Last Edit Date Time&#8221; &#8211; this field will store the date and time when the record was opened for editing.<\/li>\n<li>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\u00a0can keep the record locked indefinitely).<\/li>\n<li>Create a Javascript &#8220;Edit&#8221; button that updates the\u00a0&#8220;Last Edit Date Time&#8221; field and redirects to\u00a0the record editing page.<\/li>\n<li>Replace the standard Edit button on the page layout with the custom Edit button.<\/li>\n<\/ol>\n<p>First we create the field (this example uses Accounts):<\/p>\n<p style=\"padding-left:30px;\"><a href=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-29-17-pm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-223\" src=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-29-17-pm.png\" alt=\"Screen Shot 2014-12-30 at 6.29.17 PM\" width=\"604\" height=\"120\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-29-17-pm.png 1032w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-29-17-pm-300x60.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-29-17-pm-1024x203.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-29-17-pm-768x153.png 768w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p>Then we create the validation rule as follows:<\/p>\n<blockquote style=\"font-family:monospace;width:90%;\"><p>AND( NOW() &#8211; PRIORVALUE( Last_Edit_Date_Time__c ) &lt; ( 5\/1440 )<br \/>\n\/* locks for 5 minutes *\/<br \/>\n, PRIORVALUE( LastModifiedById ) != $User.Id )<\/p><\/blockquote>\n<p>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).<\/p>\n<p>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.<\/p>\n<p>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 &#8220;The record is locked for editing by another user (5 minutes duration).&#8221;<\/p>\n<p>Lastly, we need to create a new Button on Account to run Javascript code that updates the\u00a0Last_Edit_Date_Time__c and redirects to the\u00a0the account standard editing page:<\/p>\n<p style=\"padding-left:30px;\"><a href=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-20-18-pm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-226\" src=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-20-18-pm.png\" alt=\"Screen Shot 2014-12-30 at 7.20.18 PM\" width=\"604\" height=\"254\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-20-18-pm.png 1215w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-20-18-pm-300x126.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-20-18-pm-1024x430.png 1024w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-20-18-pm-768x322.png 768w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p>This is the Javascript code:<\/p>\n<blockquote style=\"font-family:monospace;width:90%;\"><p><code>{!REQUIRESCRIPT(\"\/soap\/ajax\/29.0\/connection.js\")}<\/p>\n<p>\/\/ prepare account for updating the last edit date time<br \/>\nvar anAccount = new sforce.SObject( \"Account\" );<br \/>\nanAccount.id = '{!Account.Id}';<br \/>\nvar dtNow = new Date();<br \/>\nvar nowInJSON = dtNow.toJSON();<br \/>\nanAccount.Last_Edit_Date_Time__c = nowInJSON;<\/p>\n<p>\/\/ update the account<br \/>\nvar result = sforce.connection.update( [ anAccount ] );<\/p>\n<p>window.location.href = '\/{!Account.Id}\/e';<\/code><\/p><\/blockquote>\n<p>Now we need to just remove the standard Edit button and place the new custom Edit button on the page layout.<\/p>\n<p style=\"padding-left:30px;\"><a href=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-25-41-pm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-227\" src=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-25-41-pm.png\" alt=\"Screen Shot 2014-12-30 at 7.25.41 PM\" width=\"604\" height=\"464\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-25-41-pm.png 845w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-25-41-pm-300x230.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-7-25-41-pm-768x590.png 768w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p>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\u00a0appear:<\/p>\n<p style=\"padding-left:30px;\"><a href=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-02-45-pm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-228\" src=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-02-45-pm.png\" alt=\"Screen Shot 2014-12-30 at 6.02.45 PM\" width=\"604\" height=\"112\" srcset=\"https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-02-45-pm.png 850w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-02-45-pm-300x56.png 300w, https:\/\/blog.felineflock.com\/wp-content\/uploads\/2014\/12\/screen-shot-2014-12-30-at-6-02-45-pm-768x143.png 768w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When 2 users start\u00a0editing 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 &#8220;The record you were editing was modified by *someone* during your edit session. Please re-display the record before editing again.&#8221; This can be an inconvenience: \u00a0if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[13,18],"tags":[],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-no-category","category-validation"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":0,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}