Category Archives: validation

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

Facilitating removal of duplicate contacts

Purpose: display a link to the Merge Contacts Wizard from the search screen and from the contact screen

Too many times users search for a contact but end up creating a duplicate anyway.

Most of the time is because they use the first contact that appears and they leave blank the fields you configured in DupeCatcher.

What follows is a way that – hopefully – will make it easy for them to remember to avoid and remove duplicates.

Even if it doesn’t help with that, at least it will make it more convenient to merge duplicates by giving a link to the Merge Contacts Wizard from the search screen and from the contact screen.

1) Create a field named Merge Duplicates (Merge_Contact_URL__c) as Formula(Text) and enter the formula as:

HYPERLINK(‘/merge/conmergewizard.jsp?id=’ & AccountId & ‘&srch=’ & FirstName & ‘+’ & LastName, ‘Merge Duplicates’, ‘_blank’)

2) Enter Description as: Link for opening this contact in the Merge Contact Wizard

3) Enter Help Text as: Click this link if you want to find and merge duplicates for this contact

4) Then add to the Contact Search layout:

5) The convenient link will appear on your search results like this:

6) You can also create a custom button that allows the same function from the Contact itself:

7) Just remember to add the custom button to the page layout:

How to disable/enable all validation rules for data loading

While working on a recruiting application, I found a solution for being able to load data into a SalesForce application without being blocked by validation rules.

Validation rules are usually intended to be applied only when a user creates a record and not when data is being imported from an external database. In this recruiting application, candidate records go several stages in a sequence (1-lead, 2-phone, 3-applicant, 4-interview, 5-contract negotiation, etc.) and this validation rule prevented the import process from loading candidate records in a stage higher than lead.

So the solution was to create a Custom Setting of type Hierarchy with a flag/checkbox in it that disables validation rules for a given user or profile. That is, all the validation rules will include this flag and only apply when the value of this flag is enabled.

To implement it:

1) click Setup, then on the left side, click App Setup/Develop/Custom Settings.

2) click New and create your settings as hierarchy/public

3) now create a custom field of type Checkbox:  click New, select Checkbox, click Next, type the name of the field as “Disable Validation Rules”, default to Unchecked, click Next, then click Save.

4) in the Custom Setting Definition/Your App Settings screen, click Manage to start configuring the setting you just created.

5) click the New button above “Default Organization Level Value”, leave the checkbox “Disable Validation Rules” unchecked and then click Save.

6) click “Back to List”, click the New button at the bottom, select Profile or User then choose the user id or profile that will perform the data loading, then click “Disable Validation Rules” and click Save.

7) now edit the validation rule appending the expression (highlighted below)
&& NOT( $Setup.Your_App_Settings__c.Disable_Validation_Rules__c )

  8 ) click Save

9) now the validation rule will only apply if the setting checkbox
Disable Validation Rules is unchecked for your profile/user

10) you can now load data freely and then, later, re-enable all
validation rules for your profile/user by changing the custom
setting.

11) you can use this way of implementing Custom Settings on
triggers too, just use the syntax below:

 Your_App_Settings__c s = 
 Your_App_Settings__c.getInstance( UserInfo.GetUserID() ); //or Profile
 if( s.Disable_Validation_Rules__c ) return; // skip trigger...

Pertinent Articles:
– Accessing Custom Settings
Let’s make some magic…