Tuesday 30 December 2014

Make Rich Text Field Mandatory Using Validation Rule


If you want to make field of data type Rich Text mandatory, what would you do?

One way to achieve this is by making field mandatory from page layout, that will work fine if you are creating records from salesforce UI but that won't work when you will upload data using Data-loader.



What will be the next approach, writing validation rule ? 
In validation rule you going to use ISNULL or ISBLANK ?

Lets try with ISNULL and ISBLANK one by one

1. ISNULL : Determine if expression is null (blank) and return TRUE if it is. If it contains value, this function returns FALSE.

create a validation rule and check ISNULL() for that rich text field like below snapshot.



in validation rule I have checked ISNULL(Rich_Text__c) and saved it. 
Now try to save the record without entering any value in Rich_Text__c field, validation rule won't work.

Why ISNULL didn't work?
According to Salesforce Text Fields are never null, so this function will always return FALSE.
Ok, then we should try with ISBLANK().

2. ISBLANK : Determines if an expression has a value and returns TRUE if it does not. If it contains a value this function will returns FALSE.

We will update validation rule from ISNULL to ISBLANK for rich text field like below snapshot.


validation rule : ISBLANK(Rich_Text__c)
Now try to save a record without entering any value in RIch_Text__c field, validation rule won't work in this case as well.
Salesforce doc says for text fields use ISBLANK, then why our validation rule is not working, Strange.

To make our validation rule to work we will use salesforce another function LEN()

3. LEN : returns the number of characters in specified test string
modify your validation to 
LEN( Rich_Text__c ) = 0


above validation will work


NOTE: above validation will work for text but won't work if you insert only
image and no text. 










No comments:

Post a Comment