Catalog Item – How to calculate total bill from multiple price fields

neehas710
Tera Contributor

Hi Community,

I am working on a Service Catalog Item that allows the user to select food items (Veg and Non-Veg) with corresponding prices.

I have almost completed all the configuration:

  • Field visibility is controlled by UI Policies based on selection of Veg or Non-Veg.

  • Prices are stored in readonly single-line text variables, for example Veg Manchuria Price = 300.

  • Each item is a checkbox, and when selected, its price field is displayed.

  • There is also an Available Offers field with choice values 10 percent, 20 percent, and 30 percent that should apply a discount to the total.

Where I am stuck:
I need to get all the selected item prices, sum them up, apply the offer if any, and display the final value in the Total Bill readonly field.

I am unsure how to:

  1. Retrieve the prices from all visible and checked items in a client-side script.

  2. Sum them dynamically when items are checked or unchecked.

  3. Apply the discount percentage from the Available Offers field.

Example of my setup:
Veg Manchuria (checkbox) and Veg Manchuria Price = 300
Chicken Biryani (checkbox) and Chicken Biryani Price = 400
Mutton Biryani (checkbox) and Mutton Biryani Price = 600
Total Bill should sum all selected prices and apply discount.

Question:
What is the best way to achieve this in a ServiceNow Catalog Client Script? Should I loop through variable names in an onChange script for each checkbox, or is there a more efficient way?

Thanks in advance for your guidance.

5 REPLIES 5

RukhsanaN
Mega Contributor

Hi Neeha! You can calculate the total by writing a small onChange client script. Add all the selected checkbox values, convert them to integers, and store the sum in a total variable. Then apply your discount logic before displaying it. Example:

var total = parseInt(g_form.getValue('item1')) + parseInt(g_form.getValue('item2'));
var discount = parseInt(g_form.getValue('discount'));
var finalTotal = total - (total * discount / 100);
g_form.setValue('total_price', finalTotal);

This should solve it!

https://fescoebillcheck.pk/