Implementing Validation Rules Using Client Scripts in Zoho CRM

Implementing Validation Rules Using Client Scripts in Zoho CRM

Problem:

The limited number of validation rules with custom functions in Zoho CRM (three in the Enterprise Plan and five in the Ultimate Plan), along with the restriction of triggering only one field per rule, significantly hinders businesses that require extensive data validation and automation. This limitation affects data quality, enforcement of business rules, and process efficiency, making it challenging to maintain accurate and consistent data entries.

Solution:

We can overcome the above problem using client script functions in Zoho CRM. By leveraging client scripts, we can implement custom validation functions to enforce complex validation rules across multiple fields and modules. This solution allows us to ensure consistent data restrictions and maintain high data accuracy. Client scripts provide the flexibility and control needed to enhance data quality and ensure compliance with our data entry standards. Implementing this approach will significantly improve the reliability and integrity of our CRM data.

To understand more about client script, you can refer this URL

Usecase:

One practical example of leveraging client script functions in Zoho CRM is implementing date validation to prevent future dates from being entered in multiple date fields. By using client scripts, we can create a custom validation function that consistently enforces this rule across various modules and fields. This ensures that users adhere to data entry standards, enhancing the accuracy and reliability of our CRM data.

Steps to achieve this:

 

Open  Zoho CRM Settings > Developer Hub > Client Script > New Script.


Select the Category as module , Page as (Edit Page (Standard)), Select the module where you want to execute the client script, Layout as Standard, Event type as Page Event and Event as onsave click next. It will take you to the page where you  want to write the code.


  1. // Define the date fields to be validated
  2. let dateFields = ['Date_1', 'Date_2', 'Date_3', 'Date_4', 'Date_5', 'Date_6'];

  3. // Initialize an array to store the field values
  4. let values2 = [];

  5. // Loop through each date field to get its value
  6. for (let i = 0; i < dateFields.length; i++) {
  7.   // Get the value of the current date field
  8.   let fieldValue = ZDK.Page.getField(dateFields[i]).getValue();
  9.   
  10.   // Check if the field value is empty or undefined
  11.   if (!fieldValue) {
  12.     // Assign '0' if the field value is empty or undefined
  13.     fieldValue = '0';
  14.   }
  15.   
  16.   // Log the field value for debugging purposes
  17.   log(fieldValue);
  18.   
  19.   // Add the field value to the values2 array
  20.   values2.push(fieldValue);
  21. }

  22. // Log all the collected field values
  23. log('values' + values2);

  24. // Function to identify fields with future dates
  25. function getFutureDateFields(dates, fields) {
  26.     // Get the current date
  27.     let currentDate = new Date();
  28.     
  29.     // Initialize an array to store fields with future dates
  30.     let futureDateFields = [];

  31.     // Loop through the dates array
  32.     for (let i = 0; i < dates.length; i++) {
  33.         // Convert the date string to a Date object
  34.         let date = new Date(dates[i]);
  35.         
  36.         // Check if the date is in the future
  37.         if (date > currentDate) {
  38.             // Add the corresponding field to the futureDateFields array
  39.             futureDateFields.push(fields[i]);
  40.         }
  41.     }

  42.     // Return the array of fields with future dates
  43.     return futureDateFields;
  44. }

  45. // Get the fields that have future dates
  46. let futureFields = getFutureDateFields(values2, dateFields);

  47. // Log the fields that have future dates
  48. log('Fields with future dates: ' + futureFields);

  49. // If there are any fields with future dates, take action
  50. if (futureFields.length > 0) {
  51.   // Loop through each field with a future date
  52.   for (let i = 0; i < futureFields.length; i++) {
  53.     // Set the value of the field to null
  54.     ZDK.Page.getField(futureFields[i]).setValue(null);
  55.     
  56.     // Make the field mandatory
  57.     ZDK.Page.getField(futureFields[i]).setMandatory(true);
  58.   }
  59.   
  60.   // Show an alert to the user listing the fields that cannot have future dates
  61.   ZDK.Client.showAlert(futureFields.join('\n'), 'Below fields cannot have a future date');
  62. }


Let us know if any updates, improvements and support in creating similar usecases.
    Zoho Desk Resources

    • Desk Community Learning Series


    • Digest


    • Functions


    • Meetups


    • Kbase


    • Resources


    • Glossary


    • Desk Marketplace


    • MVP Corner


    • Word of the Day


      Zoho CRM Plus Resources

        Zoho Books Resources


          Zoho Subscriptions Resources

            Zoho Projects Resources


              Zoho Sprints Resources


                Zoho Orchestly Resources


                  Zoho Creator Resources


                    Zoho WorkDrive Resources



                      Zoho Campaigns Resources

                        Zoho CRM Resources

                        • CRM Community Learning Series

                          CRM Community Learning Series


                        • Tips

                          Tips

                        • Functions

                          Functions

                        • Meetups

                          Meetups

                        • Kbase

                          Kbase

                        • Resources

                          Resources

                        • Digest

                          Digest

                        • CRM Marketplace

                          CRM Marketplace

                        • MVP Corner

                          MVP Corner




                          Zoho Writer Writer

                          Get Started. Write Away!

                          Writer is a powerful online word processor, designed for collaborative work.

                            Zoho CRM コンテンツ




                              ご検討中の方