Validation Rule Using Functions | Online Help - Zoho CRM

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. 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.


    Zoho CRM Training Programs

    Learn how to use the best tools for sales force automation and better customer engagement from Zoho's implementation specialists.

    Zoho CRM Training
      Redefine the way you work
      with Zoho Workplace

        Zoho DataPrep Personalized Demo

        If you'd like a personalized walk-through of our data preparation tool, please request a demo and we'll be happy to show you how to get the best out of Zoho DataPrep.

        Zoho CRM Training

          Create, share, and deliver

          beautiful slides from anywhere.

          Get Started Now


            Zoho Sign now offers specialized one-on-one training for both administrators and developers.

            BOOK A SESSION





                        Still can't find what you're looking for?

                        Write to us:  support@zohoforms.com


                              



                            



                          Manage your brands on social media

                              Zoho Marketing Automation

                                Zoho Sheet Resources

                                 




                                    Zoho Forms Resources


                                      Secure your business
                                      communication with Zoho Mail


                                      Mail on the move with
                                      Zoho Mail mobile application

                                        Stay on top of your schedule
                                        at all times


                                        Carry your calendar with you
                                        Anytime, anywhere




                                              Zoho Sign Resources

                                                Sign, Paperless!

                                                Sign and send business documents on the go!

                                                Get Started Now


                                                    Zoho SalesIQ Resources



                                                        Zoho TeamInbox Resources



                                                                Zoho DataPrep Resources



                                                                  Zoho DataPrep Demo

                                                                  Get a personalized demo or POC

                                                                  REGISTER NOW


                                                                    Design. Discuss. Deliver.

                                                                    Create visually engaging stories with Zoho Show.

                                                                    Get Started Now










                                                                                          • Related Articles

                                                                                          • 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 ...
                                                                                          • Points to note while updating a field used in a validation rule.

                                                                                            Validation Rules are effective on creating a record manually. However, when fields used in a validation rule (whether primary or secondary) are updated through other ways such as workflow update, and APIs, the field update takes precendence over the ...
                                                                                          • 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 ...
                                                                                          • FAQs on Workflow Rules

                                                                                            What is a workflow rule? Workflow rules in Zoho CRM are designed to enhance productivity of the users and reduce manual work by automating routine activities in a business process. They allow you to create rules that can trigger different action ...
                                                                                          • Working with Validation Rules

                                                                                            One of the most challenging tasks in CRM system is ensuring the validity of data in it. A poorly maintained set of data can cost a company in more ways than one - your activities are slowed down, you will have inaccurate reports and it's plain ...
                                                                                          Wherever you are is as good as
                                                                                          your workplace

                                                                                            Resources

                                                                                            Videos

                                                                                            Watch comprehensive videos on features and other important topics that will help you master Zoho CRM.



                                                                                            eBooks

                                                                                            Download free eBooks and access a range of topics to get deeper insight on successfully using Zoho CRM.



                                                                                            Webinars

                                                                                            Sign up for our webinars and learn the Zoho CRM basics, from customization to sales force automation and more.



                                                                                            CRM Tips

                                                                                            Make the most of Zoho CRM with these useful tips.



                                                                                              Zoho Show Resources