To write a validation rule in Salesforce, you will need to use the Salesforce platform and follow the guidelines provided by Salesforce for creating validation rules. This typically involves defining a validation rule formula that specifies the conditions that must be met in order for a record to be saved, and a validation rule error message that will be displayed if the formula evaluates to true.
Here is an example of what a validation rule formula might look like:
AND( ISBLANK( Phone ), ISBLANK( MobilePhone ) )
This validation rule formula checks that either the Phone field or the MobilePhone field is not blank. If both fields are blank, the formula will evaluate to true and the validation rule will be triggered.
To write a validation rule in Salesforce, follow these steps:
- Navigate to the Object Manager: In the Setup menu, go to “Object Manager” and select the object that you want to create a validation rule for.
- Click the “Validation Rules” button: In the object’s detail page, click the “Validation Rules” button in the left-hand menu.
- Click the “New” button: This will open the “New Validation Rule” page.
- Enter a name and description for the validation rule: In the “Validation Rule Name” field, enter a name for the validation rule. This should be a short, descriptive name that describes the purpose of the validation rule. In the “Validation Rule Description” field, enter a description of the validation rule. This should provide more detail about what the validation rule is checking for and why it is necessary.
- Enter the validation rule formula: In the “Validation Rule Formula” field, enter the formula that will be used to evaluate whether the validation rule should be triggered.
- Enter the validation rule error message: In the “Error Message” field, enter the message that will be displayed if the validation rule formula evaluates to true.
- Select the error location: In the “Error Location” field, select whether the error message should be displayed next to the field that caused the validation rule to be triggered, or at the top of the page.
- Save the validation rule: Click the “Save” button to save the validation rule.