Creating Validation Rules Using Functions

Creating Validation Rules Using Functions

By using function you can create a validation rule where the value entered by the user needs to be verified outside the CRM database. You can even validate the fields like phone numbers, zip code and so on for a particular pattern by mentioning the appropriate pattern in the function. For example, you can write a function to validate the account number field, such that it accept values only if they adhere to this pattern: xxx-999-x00. As account numbers are confidential and most likely be stored in a government database or bank repository, you must allow CRM to verify the pattern in those third-party databases and throw an alert whenever there is a mismatch.

Scenario
You are in a retail business and use third-party logistics to handle door-to-door deliveries and bulk shipments. Every shipment is tracked using a 10-digit reference number that is stored in the latter's database. Whenever a sales rep enters an erroneous reference number in CRM, you want to prevent the record to be saved so as to avoid unnecessary issues and delays in tracking the shipments during customer enquiries. You can validate the "reference number" field using function, such that the value will be verified at the logistic company's database for exact match. The record will then be saved only if the rep enters a 10-digit value in the appropriate format (xxx-9999999999) mentioned in the validation rule, otherwise an error be thrown showing invalid data. 



/* The snippet below shows you how to get a list of fields, their values from a MAP object. The fields’ values can be obtained from the same MAP object. */
entityMap = crmAPIRequest.toMap().get("record");
/* The example below demonstrates how a field’s value (email) can be obtained from a MAP object. Here, entityMap - Map Object, Email - Field's API name
Sample entityMap= {'Email': 'xxx@xxx.com', 'Last_Name': 'xxx'}; */
referenceField= entityMap.get("Reference_Number"); // Reference_Number is the API name for "Reference number" field.
response = Map();
/* ---------------------------------------------------------------------------------------------- */
Start writing your code here to perform the necessary field validation *
/* ---------------------------------------------------------------------------------------------- */
/* If the code identifies a validation error, set the status and alert message as shown below: */
if(referenceField.matches("[A-Z]{3}-[0-9]{10}")) {
      response.put('status','error');
      response.put('message', 'Invalid Reference Number');
} else {
/* If there are no errors found during validation, set the status as shown below: */
      response.put('status','success');
}
/* ---------------------------------------------------------------------------------------------- */
return response.toString();
Note
  1. You can change the criteria-based validation rule of the existing records into function-based validation rule by writing a function that facilitates the validation rule.
  2. The total number of validation rules that can be created depends on the subscribed edition. Out of the total number of rules/modules, you can create 3 validation rules using functions in the Enterprise edition, and 5 rules using functions in the Ultimate edition. To know the total number of rules for each edition, check the Feature Availability page.
  3. Only the fields that are supported in the criteria-based validation rule will be available in the function-based rule too.
  4. To avoid failure of the function, its advisable to give empty value checks as sometimes the fields are blank (fields without value). 
  5. Return value from the function should be as mentioned in the sample code. Any value other than the mentioned return value will be considered as an error.
  6. The field values will be available inside the function in API format only. To access the values you can use the field API name.
  7. Validation execution will be applicable in Zoho CRM mobile app too. However, you will not be able to create a rule from the mobile app.
  8. In case the function fails to execute, you will be alerted to check for the following possible reasons:
    1. Logical errors in your code.
    2. Execution timed-out causing function failure.
    3. If the issue persists, please contact support@zohocrm.com for assistance.

To create validation rule using functions
  1. Go to Setup > Customization > Modules and Fields.
  2. Select the module for which you want to create the validation rule.
  3. In the [Module] page, click the Validation Rules tab.
  4. Click Create New Validation Rule.
  5. In the Create Validation Rule pop-up, perform the following actions:
    1. Select the layout that the field you wish to validate belongs to.
    2. Select the field to validate.
    3. Select the validation type as Using function.
    4. Select either Save Only or Field Verify and Save from the Validate on dropdown.

    5. Click Next.
  6. On the Configure Function page, do either of the following:
    1. Click Functions > Choose a function > Configure > Save.
    2. Click Write your own > Enter the function > Save.
  7. Click Save to save the validation rule.


Note
The functions that are created in the Functions Tab can be associated with validation rule. 
  1. Go to Setup > Developer HubFunctions > + New Function.
  2. In the Create New Function popup, select Validation Rule from the Category drop-down list.

    • Related Articles

    • Working with Validation Rules

      Creating and keeping high-quality data is difficult, but it's even harder to identify and fix a dataset full of mistakes. Faulty CRM data can lead to negative consequences for a company, including impeding operations, generating unreliable reports, ...
    • Use Cases for Validation Rules Using Functions

      Make detailed description mandatory if specific terms are entered in the field Zylker Tech manufactures electronic assemblies and similar other products. Their manufactured items are outsourced for quality check to a third-party service provider. The ...
    • Associate Functions

      Zoho CRM Functions help you to update the data in related CRM modules or third-party applications by executing simple program scripts. When a record matches the workflow criteria, workflow engine automatically triggers the function and update the CRM ...
    • FAQs: Validation Rules

      What is a validation rule in Zoho CRM and why do I need it? In this data-driven world, it is crucial to ensure the validity and accuracy of your data. Data can enter your CRM system through various channels, such as advertisements, web forms, ...
    • Configuring Workflow Rules

      Workflow Rules in Zoho CRM, are a set of actions (email notifications, tasks and field updates) that are executed when certain specified conditions are met. These rules automate the process of sending email notifications, assigning tasks and updating ...