- 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 02:49 PM
Replace all the 'sysparam_' with 'sysparm_'
and let me know if that fixes it.
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 03:23 PM
No, it still returns null.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2022 03:26 PM
I found more issues with the scripts, please try this version (tested + working):
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 newPos = '';
var userSysID = this.getParameter('sysparm_newValue');
var userRec = new GlideRecord('sys_user');
userRec.addQuery('sys_id', userSysID);
userRec.query();
//Find User's Position
while (userRec.next()) {
newPos = userRec.title.toString();
}
//Find Position's Role
var positionsRec = new GlideRecord('u_positions');
positionsRec.addQuery('u_title', newPos);
positionsRec.query();
//Return Position's Role
while (positionsRec.next()) {
var ans = positionsRec.u_role;
}
return JSON.stringify(ans);
},
type: 'getUserPositionRole',
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Call Script Include
var userPos = new GlideAjax('getUserPositionRole');
userPos.addParam('sysparm_name', 'getPositionRole');
userPos.addParam('sysparm_newValue', newValue);
userPos.getXML(getPosRole);
//Return Employee's Position's Role
function getPosRole(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = JSON.parse(answer);
if (returneddata == 'Basic') {
alert(false);
g_form.setDisplay('accounts', false);
} else {
if (returneddata == 'Advanced') {
alert(true);
g_form.setDisplay('accounts', true);
}
}
} else {
alert('no answer');
}
}
}
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 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-04-2022 08:49 AM
Thank You!