- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-30-2021 03:50 PM
How do I call form variables from an onChange catalog client script using script includes? The returned answer I'm getting is empty:
Catalog Client Script:
if(newValue != ""){
g_form.hideFieldMsg('employee', true);
}
var ga = new GlideAjax("getEmployeeRequestedItem");
ga.addParam('sysparam_newValue', newValue);
ga.getXML(getRequestedItem);
function getRequestedItem(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == newValue){
g_form.showFieldMsg('employee', "There is already an Active Something for this Employee", 'error');
g_form.setValue('employee', "");
}
}
Script Includes:
var getEmployeeRequestedItem = Class.create();
getEmployeeRequestedItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRequestedItem: function(){
var ritmSysID = "";
var userSysID = this.getParameter('sysparam_newValue');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('active', true);
gr.addQuery('short_description', 'Something');
gr.query();
while(gr.next()){
if(gr.variables.employee == userSysID){
ritmSysID = gr.variables.employee;
break;
}
}
return ritmSysID;
},
type: 'getEmployeeRequestedItem'
});
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2021 12:59 AM
Fixed 2 items in client script
1. Client script needs to specify the name of function in Script Include.
2. Changed order of .setValue() and .showFieldMsg(). Setting showFieldMsg() and then .setValue() will hide field message.
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("getEmployeeRequestedItem");
ga.addParam('sysparm_name', 'getRequestedItem');
ga.addParam('sysparam_newValue', newValue);
ga.getXML(getRequestedItem);
function getRequestedItem(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == newValue) {
g_form.setValue('employee', ""); // need to set value to empty before setting error message
g_form.showFieldMsg('employee', "There is already an Active Something for this Employee", 'error');
}
}
}
Script Include:
var getEmployeeRequestedItem = Class.create();
getEmployeeRequestedItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRequestedItem: function() {
var ritmSysID = "";
var userSysID = this.getParameter('sysparam_newValue');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('active', true);
gr.addQuery('short_description', 'Something');
gr.query();
while (gr.next()) {
if (gr.variables.employee == userSysID) {
return gr.variables.employee;
}
}
return ritmSysID;
},
type: 'getEmployeeRequestedItem '
});
Execution result:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2021 09:54 AM
return ritmSysID.toString(); returned a blank value as well. Thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-30-2021 08:32 PM
Hello,
Please use the forum feature: "Insert/Edit code sample" when pasting code as it keeps things organized and easier to read
Please review this GlideAjax cheat sheet as well for formatting and explanation of using GlideAjax in client script: https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...
Please also use log statements in your scripts so that you can see what's going on, else you're just coding in the dark?
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2021 09:58 AM
Thanks for the tip

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2021 03:09 PM
Tips
And you're welcome. Glad my reply was Helpful, at least.
The goal of my reply is to hope that you learn properly. The cheat sheet has a breakdown of all the components and why you need them. The logging statements help you track things down on your end, and posting the code on the forums helps us all see your organized code and makes it easier to read.
Being given exact code is nice, but I hope that you've understood what was going on and why.
Take care!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-31-2021 12:59 AM
Fixed 2 items in client script
1. Client script needs to specify the name of function in Script Include.
2. Changed order of .setValue() and .showFieldMsg(). Setting showFieldMsg() and then .setValue() will hide field message.
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("getEmployeeRequestedItem");
ga.addParam('sysparm_name', 'getRequestedItem');
ga.addParam('sysparam_newValue', newValue);
ga.getXML(getRequestedItem);
function getRequestedItem(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == newValue) {
g_form.setValue('employee', ""); // need to set value to empty before setting error message
g_form.showFieldMsg('employee', "There is already an Active Something for this Employee", 'error');
}
}
}
Script Include:
var getEmployeeRequestedItem = Class.create();
getEmployeeRequestedItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRequestedItem: function() {
var ritmSysID = "";
var userSysID = this.getParameter('sysparam_newValue');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('active', true);
gr.addQuery('short_description', 'Something');
gr.query();
while (gr.next()) {
if (gr.variables.employee == userSysID) {
return gr.variables.employee;
}
}
return ritmSysID;
},
type: 'getEmployeeRequestedItem '
});
Execution result: