Use client script to perform mathematical calculations and validations on data | Online Help | Qntrl

Business case 1: Use client scripts to perform mathematical calculations and validations on data

While designing forms to replicate finance related documents like purchase order, invoice, or reimbursement bills, our workflows might require the calculation of total cost or sum of expenses incurred. To automate such mathematical calculations and perform validations on numeric data, client scripts can be used.
 

Business Scenario

Helen is looking to automate the process of generating and approving Purchase Orders in her organization. Helen’s requirement is to calculate the sum of all the expenses incurred and validate it against a fixed budget, wherein the expenses cannot exceed the budget.

Solution

To automate this requirement, Helen uses Client Scripts in Qntrl. 
  1. Helen creates a form and adds different Currency fields to record all the expenses incurred and to declare the Budget .
  2. The sum of all expenses is calculated using client script and stored in a new Currency field, say Total Cost .
  3. The fixed Budget amount can be validated against the Total Cost using client scripts. If the Total Cost exceeds the Budget, an error message will be displayed and the user is not allowed to submit the PO. If the Total Cost is well within the Budget, the PO is submitted and sent for approval.

Sample Configuration   

Step 1: Create an orchestration

Create a new orchestration— Purchase Order Orchestration—and add related fields to it.
  1. Add Currency fields to record different expenses like Item cost, Tax, and Freight charges.
  2. Create a Currency field to calculate and display the Total cost.
  3. Create another Currency field to declare the Budget. Here, we’ve set $1000 as the default budget value. 


Once the form is created, proceed to design the blueprint, set permissions, and publish the orchestration.

Step 2: Code client scripts

Helen’s process requires 2 client scripts—one for calculating the total cost and the other to validate the cost against budget. 

Script 1: Purchase order cost calculation

  1. Create a new script and enter a name—Purchase order cost calculation.
  2. Choose Purchase Order Orchestration  in the Form  dropdown.
  3. Choose Add new card  as the Execution Location to execute the script when a new card is added.
  4. Choose onChange as the Execution Trigger to calculate the total cost whenever the expense values are changed. On selecting this option, a Fields dropdown will be displayed in the right panel, where you can choose the fields whose update must trigger the script.
  5. Copy paste the below script into your script editor and replace the parameter names.
    1. You can use the ? Icon at the top-right corner of the script editor to refer to parameter names.
  6. Once the script is ready, Publish it.
Sample Script

async function onChange(forpage,oldVal,newVal,executeOnLoad){
    if(executeOnLoad){
        return;
    }
    var item_cost = current.Job.getValue(current.Layout.Fields.<select-Item-cost-parameter-here>.id);
    var tax = current.Job.getValue(current.Layout.Fields.<select-Tax-parameter-here>.id);
    var freight = current.Job.getValue(current.Layout.Fields.<select-Freight-charges-parameter-here>.id);
    var total = item_cost.value + tax.value + freight.value;
    current.Job.setValue(current.Layout.Fields.<select-Total-cost-parameter-here>.id, total);



Note
  1. You can either choose Add new card  or Card details or both as the Execution
    Location based on where you want the validation to be performed.
  2. Also, you can either choose onChange or onSubmit as the Execution Trigger .
    1. Selecting onChange will enable the script to calculate the total cost whenever the expense values are changed.
    2. Selecting onSubmit will allow the script to calculate the total cost when the card is submitted or saved. 

Script 2: Purchase order budget validation

  1. Create a new script and enter a name—Purchase order budget validation.
  2. Choose Purchase Order Orchestration  in the Form  dropdown.
  3. Choose Add new card  as the Execution Location to execute the script when a new card is added.
  4. Choose onSubmit as the Execution Trigger to enable the script to validate the total cost against the budget whenever the new card is submitted by clicking the Save button.
  5. Copy paste the below script into your script editor and replace the parameter names.
    1. You can use the ? Icon at the top-right corner of the script editor to refer to parameter names.
  6. Once the script is ready, Publish it. 
Sample Script

async function onSubmit(forpage) {
    var total = current.Job.getValue(current.Layout.Fields.<select-Total-cost-parameter-here>.id);
    var budget = current.Job.getValue(current.Layout.Fields.<select-Budget-parameter-here>.id);
    if (total.value > budget.value) {
        ZDK.Message.Inline(current.Layout.Fields.<select-Total-cost-parameter-here>.id, "Total cost exceeds the Budget");
        return false;
    }
  return true;
}


Note
  1. You can either choose Add new card  or Card details or both as the Execution Location based on where you want the validation to be performed.
  2. Also, you can either choose onChange or onSubmit as the Execution Trigger .
    1. Selecting onChange will enable the script to validate the budget whenever the expense values or total cost is changed.
    2. Selecting onSubmit will allow the script to validate the budget when the card is submitted or saved. 

Step 3: Add card

Once the form, blueprint, and client scripts are ready, we can test the script by creating a new card. 
  1. Navigate to Cards and click Add Card .
  2. Select Purchase Order Orchestration  from the Form dropdown.
  3. Enter the card details and expenses incurred.
    1. Total cost gets calculated automatically.
    2. If the total cost is higher than the budget, an error message is displayed.  




    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


            Get started with Zoho Sign

            in a few quick steps!

            Download Help Guide





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

                      Write to us: support@zohoforms.com


                            




                            

                          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

                                                                                      • Manage Client Scripts

                                                                                        Client Scripts are used to incorporate additional validations to business data while they are recorded in Qntrl. In client scripts, you can code using JavaScript and run the code on the end-user’s browser while they are creating a new card or ...
                                                                                      • Business case 3: Use client scripts to fetch data from third party applications or websites

                                                                                        While designing forms, we might sometimes want to display data from other websites or applications like stock rates, flight status, freight details, weather updates, and so on. To fetch such information from third party websites or applications and ...
                                                                                      • Business case 2: Use client scripts to auto-populate fields based on user input

                                                                                        While designing forms, we would sometimes require to display different sets of fields for different user responses. Such automatic field population can be handled using client scripts.   Business Scenario Helen creates a sign-up form for her product ...
                                                                                      • Qntrl for iPhone

                                                                                        Carry your workspace wherever you go using Qntrl's mobile app. Users can view requests, perform cards, and track updates on the go by downloading Qntrl's iOS application.  Minimum OS version: iOS 10 Install Qntrl to your iPhone You can install Qntrl ...
                                                                                      • Create Custom Fields in Forms

                                                                                        Qntrl supports different types of custom fields. Custom fields in Qntrl follow some common properties that can be modified according to the user's requirement. Let us learn how to add different custom fields and configure its properties.  Add ...
                                                                                      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