add fields value and auto populate total on another field servicenow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2023 08:31 PM
Hi developers,
can someone help how to auto populate the total field value after user input the amounts needed , I made a code below while its not working. not sure what to input on Variable name.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var reg_cost = g_form.getValue(
"registration_cost"
);
var est_cost = g_form.getValue(
"estimated_costs"
);
var accom_cost = g_form.getValue(
"estimated_accom"
);
var meal_cost = g_form.getValue("meal_costs");
var other_cost = g_form.getVal("other_costs");
var total =
parseFloat(reg_cost) +
parseFloat(est_cost) +
parseFloat(accom_cost) +
parseFloat(meal_cost) +
parseFloat(other_cost);
g_form.setValue("total_cost", total);
alert("total");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2023 09:18 PM
Hi, the variable name would be the variable\field that is being chaned\updated in the UI, IE the field triggering the script to run. The catalog item is the catalog item\record producer you are developing.
If you are wanting to dynamically update the total cost whenever 1 of the component fields is changed you would need an onChange client script for each field.
Also, you do not have to 'getValue for the field being changed, this value is available to you in your client script as the newValue parameter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2023 09:22 PM
on which variable you are running the onChange? did you actually change that variable and verify the script?
Also are all the other variables populated or not?
script looks fine
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var reg_cost = g_form.getValue("registration_cost");
var est_cost = g_form.getValue("estimated_costs");
var accom_cost = g_form.getValue("estimated_accom");
var meal_cost = g_form.getValue("meal_costs");
var other_cost = g_form.getVal("other_costs");
var total = parseFloat(reg_cost) + parseFloat(est_cost) + parseFloat(accom_cost) + parseFloat(meal_cost) + parseFloat(other_cost);
g_form.setValue("total_cost", total);
alert("total");
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2023 09:23 PM - edited 06-14-2023 09:26 PM
I can see there are multiple different cost variables, If you want use onChange client script you'll have to write it on each cost variable for a better user experience or You can write an onSubmit client script to calculate the total cost upon submission of the request.
In the onChange client script in the variable name field, you have to pass the cost variable name
For example, When you create onChange on the Registration cost variable then the variable name in the client script will be registration_cost same goes for the rest of the variables.
onChange sample code on one cost variable that is registration_cost
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var reg_cost = newValue //Here variable name is registration_cost so, the newValue is the latest value you input into the variable.
var est_cost = g_form.getValue(
"estimated_costs"
);
var accom_cost = g_form.getValue(
"estimated_accom"
);
var meal_cost = g_form.getValue("meal_costs");
var other_cost = g_form.getVal("other_costs");
var total =
parseFloat(reg_cost) +
parseFloat(est_cost) +
parseFloat(accom_cost) +
parseFloat(meal_cost) +
parseFloat(other_cost);
g_form.setValue("total_cost", total);
alert("total");
}
If I could help you with my response you can mark it as helpful and correct as it benefits future viewers
Thanks,
Sai Kumar B
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2023 12:20 AM