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.
- Helen creates a form and adds different Currency fields to record all the expenses incurred and to declare the Budget .
- The sum of all expenses is calculated using client script and stored in a new Currency field, say Total Cost .
- 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.
- Add Currency fields to record different expenses like Item cost, Tax, and Freight charges.
- Create a Currency field to calculate and display the Total cost.
- 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
- Create a new script and enter a name—Purchase order cost calculation.
- Choose Purchase Order Orchestration in the Form dropdown.
- Choose Add new card as the Execution Location to execute the script when a new card is added.
- 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.
- Copy paste the below script into your script editor and replace the parameter names.
- You can use the ? Icon at the top-right corner of the script editor to refer to parameter names.
- 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
-
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.
- Also, you can either choose onChange or onSubmit as the Execution Trigger .
- Selecting onChange will enable the script to calculate the total cost whenever the expense values are changed.
- Selecting onSubmit will allow the script to calculate the total cost when the card is submitted or saved.
Script 2: Purchase order budget validation
- Create a new script and enter a name—Purchase order budget validation.
- Choose Purchase Order Orchestration in the Form dropdown.
- Choose Add new card as the Execution Location to execute the script when a new card is added.
- 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.
- Copy paste the below script into your script editor and replace the parameter names.
- You can use the ? Icon at the top-right corner of the script editor to refer to parameter names.
- 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
- 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.
- Also, you can either choose onChange or onSubmit as the Execution Trigger .
- Selecting onChange will enable the script to validate the budget whenever the expense values or total cost is changed.
- 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.
- Navigate to Cards and click Add Card .
- Select Purchase Order Orchestration from the Form dropdown.
- Enter the card details and expenses incurred.
- Total cost gets calculated automatically.
- If the total cost is higher than the budget, an error message is displayed.