onChange Client Script - How to get value on multiple variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 01:09 AM
How can I check the value on multiple variables on my catalog client script? My current script is only looking at newValue because on the client script form, there is only one option to choose from for variables. In addition, my second part of the script doesn't look at the right section of my script include. Is it possible to execute different information for a delivery date? Also is it possible to update the dates in real time when the values change?
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue == '' && g_form.getValue('u_category') == 'Advertisement') {
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.getXML(calThreeDays);
}
else if (newValue == 'Print & Ship' && g_form.getValue('u_category') == 'Advertisement') {
var ajax2 = new GlideAjax('ClientDateTimeUtils');
ajax2.addParam('sysparm_name', 'addDateAmount2');
ajax2.getXML(calThreeDays);
}
function calThreeDays(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_delivery_date', answer);
}
}
Script Include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
addDateAmount: function(){
var date = new GlideDateTime(gs.now());
if(date.getDayOfWeekUTC() == 2){
date.addDays(2); // if MONDAY then add 6 days to get Monday
}
else if(date.getDayOfWeekUTC() == 3){
date.addDays(2); // if TUESDAY then add 7 days to get Monday
}
else if(date.getDayOfWeekUTC() == 4){
date.addDays(4); // if WEDNESDAY then add 8 days to get Monday
}
else if(date.getDayOfWeekUTC() == 5){
date.addDays(3); // if THURSDAY then add 3 days to get Monday
}
else if(date.getDayOfWeekUTC() == 6){
date.addDays(2); // if FRIDAY then add 3 days to get Monday
}
else if(date.getDayOfWeekUTC() == 7){
date.addDays(1); // if SATURDAY then add 6 days to get Monday
}
else if(date.getDayOfWeekUTC() == 1){
date.addDays(0); // if SUNDAY then add 6 days to get Monday
}
return date.getDate();
},
addDateAmount2: function(){
var date2 = new GlideDateTime(gs.now());
if(date2.getDayOfWeekUTC() == 2){
date2.addDays(2); // if MONDAY then add 6 days to get Monday
}
else if(date2.getDayOfWeekUTC() == 3){
date2.addDays(7); // if TUESDAY then add 7 days to get Monday
}
else if(date2.getDayOfWeekUTC() == 4){
date2.addDays(4); // if WEDNESDAY then add 8 days to get Monday
}
else if(date2.getDayOfWeekUTC() == 5){
date2.addDays(3); // if THURSDAY then add 3 days to get Monday
}
else if(date2.getDayOfWeekUTC() == 6){
date2.addDays(2); // if FRIDAY then add 3 days to get Monday
}
else if(date2.getDayOfWeekUTC() == 7){
date2.addDays(1); // if SATURDAY then add 6 days to get Monday
}
else if(date2.getDayOfWeekUTC() == 1){
date2.addDays(0); // if SUNDAY then add 6 days to get Monday
}
return date2.getDate();
},
type : 'ClientDateTimeUtils'
});
- Labels:
-
Service Portal Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 08:08 PM
try to add alert to know what you get in u_category
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 10:35 PM
Kevin,
Check if there is another Script Include named "ClientDateTimeUtils". If there is a Script Include with the same name, wrong script may be executing.
I just renamed the current Script Include to "ClientDateTimeUtils2".
Client Script
function onChange(control, oldValue, newValue, isLoading) {
var outputMedia = g_form.getValue('u_output_media');
var category = g_form.getValue('u_category');
if (outputMedia == '' && category == 'Advertisement') {
var ajax = new GlideAjax('ClientDateTimeUtils2');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.getXML(calThreeDays);
} else if (outputMedia == 'Print & Ship' && category == 'Advertisement') {
var ajax2 = new GlideAjax('ClientDateTimeUtils2');
ajax2.addParam('sysparm_name', 'addDateAmount2');
ajax2.getXML(calThreeDays);
}
function calThreeDays(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_delivery_date', answer);
}
}
Script Include:
var ClientDateTimeUtils2 = Class.create();
ClientDateTimeUtils2.prototype = Object.extendsObject(AbstractAjaxProcessor, {
addDateAmount: function() {
var date = new GlideDateTime(gs.now());
if (date.getDayOfWeekUTC() == 2) {
date.addDays(2); // if MONDAY then add 6 days to get Monday
} else if (date.getDayOfWeekUTC() == 3) {
date.addDays(2); // if TUESDAY then add 7 days to get Monday
} else if (date.getDayOfWeekUTC() == 4) {
date.addDays(4); // if WEDNESDAY then add 8 days to get Monday
} else if (date.getDayOfWeekUTC() == 5) {
date.addDays(3); // if THURSDAY then add 3 days to get Monday
} else if (date.getDayOfWeekUTC() == 6) {
date.addDays(2); // if FRIDAY then add 3 days to get Monday
} else if (date.getDayOfWeekUTC() == 7) {
date.addDays(1); // if SATURDAY then add 6 days to get Monday
} else if (date.getDayOfWeekUTC() == 1) {
date.addDays(0); // if SUNDAY then add 6 days to get Monday
}
return date.getDate();
},
addDateAmount2: function() {
var date2 = new GlideDateTime(gs.now());
if (date2.getDayOfWeekUTC() == 2) {
date2.addDays(2); // if MONDAY then add 6 days to get Monday
} else if (date2.getDayOfWeekUTC() == 3) {
date2.addDays(7); // if TUESDAY then add 7 days to get Monday
} else if (date2.getDayOfWeekUTC() == 4) {
date2.addDays(4); // if WEDNESDAY then add 8 days to get Monday
} else if (date2.getDayOfWeekUTC() == 5) {
date2.addDays(3); // if THURSDAY then add 3 days to get Monday
} else if (date2.getDayOfWeekUTC() == 6) {
date2.addDays(2); // if FRIDAY then add 3 days to get Monday
} else if (date2.getDayOfWeekUTC() == 7) {
date2.addDays(1); // if SATURDAY then add 6 days to get Monday
} else if (date2.getDayOfWeekUTC() == 1) {
date2.addDays(0); // if SUNDAY then add 6 days to get Monday
}
return date2.getDate();
},
type: 'ClientDateTimeUtils2'
});
Execution results (today is 2022-03-16)
1. case: u_category= "Advertisement" && Output Media is empty
2. case:
_category= "Advertisement" && Output Media = 'Print & Ship'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2022 11:54 PM
Unfortunately, I'm still running into the issue where it doesn't look at that second variable. What are your other settings for the onChange client script? See mine below.
I've even tried commenting out the second part of the script and entering 'Print & Ship' on the first section, but it gets ignored.
I made sure that there was only one Script Include with the name ClientDateTimeUtils
NOTE: The code does work only for the variable > u_category , which should be due to variable name lookup field. I'm wondering how you got it to work?
function onChange(control, oldValue, newValue, isLoading) {
var outputMedia = g_form.getValue('u_output_media');
var category = g_form.getValue('u_category');
if (outputMedia == 'Print & Ship' && category == 'Advertisement') {
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.getXML(calThreeDays);
}
//else if (outputMedia == 'Print & Ship' && category == 'Advertisement') {
//var ajax2 = new GlideAjax('ClientDateTimeUtils');
//ajax2.addParam('sysparm_name', 'addDateAmount2');
//ajax2.getXML(calThreeDays);
//}
function calThreeDays(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_delivery_date', answer);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2022 12:55 AM
Check the value of u_output_media selection is "Print & Ship" literally with spaces and with the right upper/lower case. I've made it into a Select Box. Since the category == 'Advertisement' is working, it's probably the u_output_media.
Note that the value to compare is not the Text but the Value. Try copy & pasting the value from the definition to the script in case there's some special character. It's usually better to not have spaces in the Value (i.e. make the value into "PrintAndShip".