- 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 10:13 AM
This worked perfectly! Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2021 04:39 AM
Hi,
Please follow the steps below to achieve your requirement:
Advantage and Recommended approach as this is much easier to check for any Variable Value on RITM form rather than using a Script include and is faster due to very less code execution required here.
1) There is a much simpler way to handle this. Please create a Display Business Rule on Requested item table and use the script below for reference where you can easily compare the Variable value you want.
(function executeRule(current, previous /*null when async*/) {
// Add your code here.
g_scratchpad.EMPLOYEE = current.variables.employee;
})(current, previous);
You can give the condition same as your Script Include like you are checking the Short Description as shown below:
Now in your Client Script you can directly reference this Variable and use it as shown below:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (g_scratchpad.EMPLOYEE == newValue) {
g_form.showFieldMsg('employee', "There is already an Active Something for this Employee", 'error');
g_form.setValue('employee', "");
}
//Type appropriate comment here, and begin script below
}
g_scratchpad is used in ServiceNow for mostly to handle scenario like yours, if it's really needed then would recommend go with Script Include approach.
Let me know if you are stuck.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke