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. You can create 3 rules per module.
  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. Hover on the Module, click the More icon and select Set Validation rules.
  3. Under Validation Rule tab, click New Validation Rule.
  4. In Create Validation Rule page, under Choose field to validate select a field from the drop-down list.
  5. In Choose Validation type, check Validate using function.
  6. Click Next.

  7. In Configure Function page, do either of the following:
    1. Click Functions > Choose a function Configure > Save.
    2. Click Create your own > Enter the function > Save.
  8. Click Save, to save the validation rule. 
Note
The functions that are created under CRM Fundamentals (in Functions Tab) can be associated with validation rule. 
  1. Go to Setup > Developer Space > Function > Create New Functions.
  2. In Choose a place, select CRM Fundamentals from the drop-down list.


    • Related Articles

    • Validation Rules

      Ensure the accuracy of data that enters CRM using Validation Rules Help guide Working with Validation Rules Exceptions to Validation Rules
    • 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 ...
    • Functions

      Set Up Functions   You can set up functions in six places: Create a standalone function. Associate function to a workflow rule. Use functions later by including them in Schedules. Set the function to get executed upon the click of a Custom Button. ...
    • Functions

      Functions are the building blocks for developing an extension. They come handy for all server side customization. Sigma executes functions without additional infrastructure cost. External function calls can be done using rest APIs. You have to ...
    • Workflow Rules

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