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 a board

Create a new board— Purchase Order Board—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 board.

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 Board   in the Board drop-down.
  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 drop-down 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 Board   in the Board drop-down.
  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 Boards and select a board from the left panel.
  2. Click Add Card.
  3. Select Purchase Order Board   from the Board drop-down.
  4. 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.  




    • 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 ...
    • Advanced Fields

      Lookups Lookups are fields used to obtain a subset of options from available values in fields, tables, webhook API responses, or database query responses. Existing data available in the organization can be reused using lookups. Lookups usually return ...
    • Conditional Fields

      This feature is open only on request. If you would like to try it, email our support team at support@qntrl.com. In specific business scenarios, we would want certain fields or sections of the form to be displayed based on the input a user enters in ...

    You are currently viewing the help articles of Qntrl 3.0. If you are still using our older version and require guidance with it, Click here.