- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 01:28 PM
When I test this script in the Service Catalog, the alert returns null, and the "accounts' field doesn't get displayed. This script runs when the Employee (Reference sys_user table) changes.
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Call Script Include
var gr = new GlideAjax("getUserPositionRole");
gr.addParam('sysparm_name', 'getPositionRole');
gr.addParam('sysparam_newValue', newValue);
gr.getXML(getPosRole);
//Return Employee's Position's Role
function getPosRole(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer == "Basic"){
g_form.setDisplay("accounts", false);
}else if(answer == "Advanced"){
g_form.setDisplay("accounts", true);
}
}
}
Script Includes:
var getUserPositionRole = Class.create();
getUserPositionRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Find The User's Position and return the Position's Role
getPositionRole: function(){
var newPos = "";
var userSysID = this.getParameter('sysparam_newValue');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', sysparam_newValue);
gr.query();
//Find User's Position
while(gr.next()){
newPos = gr.title;
}
//Find Position's Role
var gr2 = new GlideRecord('u_positions');
gr2.addQuery('u_title', newPos);
gr2.query();
//Return Position's Role
while(gr2.next()){
return gr2.u_role;
}
},
type: 'getUserPositionRole'
});
What am I missing here? Thanks.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 03:24 PM
Try the following.
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Call Script Include
var gr = new GlideAjax("getUserPositionRole");
gr.addParam('sysparm_name', 'getPositionRole');
gr.addParam('sysparm_newValue', newValue);
gr.getXMLAnswer(function(answer) {
alert(answer);
if (answer == "Basic") {
g_form.setDisplay("accounts", false);
} else if (answer == "Advanced") {
g_form.setDisplay("accounts", true);
}
});
}
Script Include:
var getUserPositionRole = Class.create();
getUserPositionRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Find The User's Position and return the Position's Role
getPositionRole: function() {
var userSysID = this.getParameter('sysparm_newValue');
var gr = new GlideRecord('sys_user');
if (gr.get(userSysID)) {
var gr2 = new GlideRecord('u_positions');
if (gr2.get('u_title', gr.title.toString())) {
return gr2.u_role.toString();
}
}
},
type: 'getUserPositionRole'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 01:38 PM
Hi Jeff,
in the line gr2.addQuery('u_title', newTitle); I don't see a variable newTitle in your code.
Did you mean to write newPos?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 02:25 PM
Yes, I meant to write newPos. I edited the script to when posting. I have updated it, and the script still returns null. Thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 01:47 PM
You are assigning gr.title to newPos, so i've updated your script to reflect that
Try:
var getUserPositionRole = Class.create();
getUserPositionRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Find The User's Position and return the Position's Role
getPositionRole: function(){
var newPos = "";
var userSysID = this.getParameter('sysparam_newValue');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', sysparam_newValue);
gr.query();
//Find User's Position
while(gr.next()){
newPos = gr.title;
}
//Find Position's Role
var gr2 = new GlideRecord('u_positions');
gr2.addQuery('u_title', newPos);
gr2.query();
//Return Position's Role
while(gr2.next()){
return gr2.u_role;
}
},
type: 'getUserPositionRole'
});
Please mark my answer as Correct/Helpful based on impact
Regards,
Dan H
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 02:26 PM
Thanks. I meant to write newPos and I edited the script to when posting. I have updated it, and the script still returns null. Thanks.