Object Validation Rule Examples

Below are some examples of object validation rules and their use cases. We know that many of our users are not familiar with C# (C sharp) or any other C programming language. Don't worry - you can start using these formulas even if you have no coding knowledge.

Just copy and paste the formulas below into the Rule Formula field when you are creating a rule. Each rule you create is object-specific. If you want a rule to work across multiple object types, you'll need to create a rule for each object.

Object Validation Rule Examples

Disclaimer: Insightly provides this article for informational purposes only. 

Rule #1 - Record Name needs to be valid

Reject if 'First Name' = "Test".

if (Record.FIRST_NAME == "Test"){   return Validation.IsError;} else {  return Validation.IsValid; }

This formula will prevent your users from using "Test" as the first name of a record they are creating or editing.

You can replace "Test" with any text you want if you want to prevent your users from entering other names.

Rule #2: Contact must contain an Organization

Reject if 'Organization' is empty.

if(Record.ORGANISATION_ID != null){ return Validation.IsValid;}else{ return Validation.IsError;}

If your users have a habit of forgetting to link the Contacts they create to Organizations, use this formula to prevent them from saving until they add an Organization.

Rule #3: Email Address cannot contain Gmail

Reject if ‘Email’ contains "gmail".

var test = Record.EMAIL_ADDRESS?.Contains("gmail") ?? false;

if (test == true){
  return Validation.IsError;
}
else {
  return Validation.IsValid;
}

When you want your users to only accept business-domain email addresses, this formula will prevent them from entering an address that contains "gmail", which is likely a personal email.

Rule #4: Max discounted rate is 15%

Reject if discount offered is more than 15%.

if(Record.Discount_Offered__c > 15){ return Validation.IsError;}else{ return Validation.IsValid;}

If you use Products, Price Books, and Quotes, you can use this formula to prevent users from giving a discount larger than 15%.

Rule #5: Task due date cannot be before the start date

Reject if the due date is before the start date.

if(Record.DUE_DATE != null && Record.DUE_DATE < Record.START_DATE){ return Validation.IsError;}else{ return Validation.IsValid;}

This formula will reject Tasks that have a due date that comes before the start date. This formula can be used to prevent errors.

Rule #6: Timecards must total 40 hours

Reject if total hours exceed 40.

 if(Monday_Hours__c + Tuesday_Hours__c + Wednesday_Hours__c + Thursday_Hours__c + Friday_Hours__c > 40){Return Validation.isError;}else{return Validation.isValid;}

Use this formula to ensure all time cards are recorded as standard 40 hour work weeks. This will help employees under or over-reporting the time they've worked.

Rule #7: Check the number of digits in a numeric field

Check the number of digits a numeric field has. Reject if the length is greater than 10.

String a = Record.[Field_Name].ToString();if(a.Length >= 10){return Validation.IsValid;}else{return Validation.IsError;}

Use this formula to check the number of digits entered into a numeric field. In the code above, if the total number of digits exceeds 10, the field will be rejected. This can be used to ensure that any phone numbers that are entered will be valid.

Rule #8: Date/Time field cannot be empty/null

 Checks the a Date/Time field. Reject if the field is empty or null.

if (Record.DATE_OF_BIRTH == null){  //show validation error if Contact's Date of Birth is empty  return Validation.IsError;}return Validation.IsValid;

Use this formula to check if a date/time field is empty or null. In the code above, if the Date of Birth field is empty, it will be rejected.

Rule #9: Disallows specific fields from being selected in a dropdown

Checks field values and rejects if the user selects a specific field value.

if (Record.Dropdown_Field__c == "Error"){ //show validation error when field value is equal to "Error" return Validation.IsError;}return Validation.IsValid;

This formula is used to prevent users from selecting specific field values from a dropdown. In the code above, users cannot select the field value "Error".

 

Was this article helpful?