Newer Version Available

This content describes an older version of this product. View Latest

ValidationRule

Represents a validation rule, which is used to verify that the data a user enters in a record is valid and can be saved. A validation rule contains a formula or expression that evaluates the data in one or more fields and returns a value of true or false. Validation rules also include an error message that your client application can display to the user when the rule returns a value of true due to invalid data. This type extends the Metadata metadata type and inherits its fullName field.

As of API version 20.0, validation rules can't have compound fields. Examples of compound fields include addresses, first and last names, dependent picklists, and dependent lookups.

Version

Validation rules are available in API version 12.0 and later.

Fields

Field Name Field Type Description
active boolean Required. Indicates whether this validation rule is active, (true), or not active (false).
description string A description of the validation rule.
errorConditionFormula string Required. The formula defined in the validation rule. If the formula returns a value of true, an error message is displayed.

See “Define Validation Rules” in the Salesforce online help.

errorDisplayField string The fully specified name of a field in the application. If a value is supplied, the error message appears next to the specified field. If you do not specify a value or the field isn’t visible on the page layout, the value changes automatically to Top of Page.
errorMessage string Required. The message that appears if the validation rule fails. The message must be 255 characters or less.
fullName string The internal name of the object. White spaces and special characters are escaped for validity. The name must:
  • Contain characters, letters, or the underscore (_) character
  • Must start with a letter
  • Can’t end with an underscore
  • Can't contain two consecutive underscore characters.

Inherited from the Metadata component, this field is not defined in the WSDL for this component. It must be specified when creating, updating, or deleting. See create() to see an example of this field specified for a call.

Declarative Metadata Sample Definition

A sample XML definition of a validation rule in a custom object is shown below.

1<?xml version="1.0" encoding="UTF-8"?>
2<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
3    <deploymentStatus>Deployed</deploymentStatus>
4    <fields>
5        <fullName>Mommy_Cat__c</fullName>
6        <label>Mommy Cat</label>
7        <referenceTo>Cat__c</referenceTo>
8        <relationshipName>Cats</relationshipName>
9        <type>Lookup</type>
10    </fields>
11    <label>Cat</label>
12    <nameField>
13        <label>Cat Name</label>
14        <type>Text</type>
15    </nameField>
16    <pluralLabel>Cats</pluralLabel>
17    <sharingModel>ReadWrite</sharingModel>
18    <validationRules>
19        <fullName>CatsRule</fullName>
20        <active>true</active>
21        <errorConditionFormula>OR(Name = &apos;Milo&apos;,Name = &apos;Moop&apos;)</errorConditionFormula>
22        <validationMessage>Name must be that of one of my cats</validationMessage>
23    </validationRules>
24</CustomObject>