- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 06:18 AM
i created a request for a Copier Request and need help configuring a total variable that updates based on two other variables.
- There are two single text variables:
- Black & White Pages
- Color Pages
- I need a Total Pages variable that:
- Automatically updates as users enter values for the Black & White and Color Pages
- Displays the sum of the two variables
What’s the best approach to achieve this in ServiceNow’s Catalog Item setup. i attached the 2 catalog
black and white client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == oldValue) {
return;
}
// Get values from the fields
var blackWhite = parseInt(g_form.getValue('black_and_white')) || 0;
var color = parseInt(g_form.getValue('color')) || 0;
// Calculate total
var total = blackWhite + color;
// Update the total_copies field
g_form.setValue('total_copies', total);
}
color at client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == oldValue) {
return;
}
// Get values from the fields
var blackWhite = parseInt(g_form.getValue('black_and_white')) || 0;
var color = parseInt(g_form.getValue('color')) || 0;
// Calculate total
var total = blackWhite + color;
// Update the total_copies field
g_form.setValue('total_copies', total);
}
client scripts but only the black and white script seems to be working and updating dynamically when changed
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 06:45 AM
use this for both the scripts and see
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (oldValue != newValue) {
// Get values from the fields
var blackWhite = parseInt(g_form.getValue('black_and_white')) || 0;
var color = parseInt(g_form.getValue('color')) || 0;
// Calculate total
var total = blackWhite + color;
// Update the total_copies field
g_form.setValue('total_copies', total);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 06:26 AM
you will require 2 onchange 1 each on each variable
both script looks fine.
did you add alert and see?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 06:36 AM
Hi yes, there is one for both variables. i opened the console and see the onchage is triggering for the 1st (black _and_white) variable but not for the color. that's the issue I'm seeing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 06:45 AM
use this for both the scripts and see
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (oldValue != newValue) {
// Get values from the fields
var blackWhite = parseInt(g_form.getValue('black_and_white')) || 0;
var color = parseInt(g_form.getValue('color')) || 0;
// Calculate total
var total = blackWhite + color;
// Update the total_copies field
g_form.setValue('total_copies', total);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2025 07:06 AM
If colors is changed after total copies updates for both no change. I can continuously update blackand white and it will update.