- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2016 03:32 PM
Hello Community,
I have a question. I am trying to query a certain role and return a true statement when a user has that certain role. What am I doing wrong?
Here is the script I am trying to work with
function userHasRole(UserID, role){
var roles = new GlideRecord('sys_user_has_role');
roles.addQuery('user', userID);
roles.addQuery('role.name', change_manager);
roles.query();
if (roles == change_manager){
return 'true';
}
return 'false';
}
Also you can have a function within a function right? Here is the whole code I am working on:
answer = ifScript();
function ifScript() {
//answer = 'false';
var userID = gs.getUserID();
gs.log("work start check for " + userID + " assigned_to " + current.assigned_to);
if (userID == current.assigned_to) {
return 'true'; }
// query to see if userID has role change management, if so return true
function userHasRole(UserID, role){
var roles = new GlideRecord('sys_user_has_role');
roles.addQuery('user', userID);
roles.addQuery('role.name', change_manager);
roles.query();
if (roles == change_manager){
return 'true';
}
}
}
return 'false';
Thanks in advanced.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2016 01:50 PM
Hello Everyone,
So I managed to hash it out. Here is the code:
answer = ifScript();
function ifScript() {
//answer = 'false';
var userID = gs.getUserID();
gs.log("work start check for " + userID + " assigned_to " + current.assigned_to);
if (userID == current.assigned_to) {
return 'true'; }
// query to see if userID has role change management, if so return true
var cm_role = new GlideRecord('sys_user_role');
cm_role.addQuery('name', "change_management");
cm_role.query();
while (cm_role.next()) {
var roles = new GlideRecord('sys_user_has_role');
roles.addQuery('user', userID);
roles.addQuery('role', cm_role.sys_id);
roles.query();
while (roles.next()){
gs.log("work start role check for " + userID + " has role " + cm_role.name);
return true;
}
}
gs.log("end of work start role check for " + userID + ", no writing allowed" );
return false;
}
The errors were as follows:
- I had to only use one "answer". In one variation of the code I had two answers, I could piggy back of the first answer argument.
- turns out that sys_user_has_role doesnt have a table named "name". This is why I had to query the name from the sys_user_role table while grabbing the sys_id of the name of the sys_user_role and populate that in line 20 of the code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2016 11:54 AM
Hello Jamie
Here is the updated code that you so kindly assisted me on but I am getting this log error in return. I apologize for my noob level of coding but I am learning and super appreciative of any and all advice as I always pay it forward in full.
Newly updated code:
answer = ifScript();
function ifScript() {
//answer = 'false';
var userID = gs.getUserID();
gs.log("work start check for " + userID + " assigned_to " + current.assigned_to);
if (userID == current.assigned_to) {
return 'true'; }
// query to see if userID has role change management, if so return true
answer = userHasRole();
function userHasRole(UserID, thisRole){
var UserID = current.sys_user;
var roles = new GlideRecord('sys_user_has_role');
//var UserID = gs.getUserID();
roles.addQuery('user', gs.getUserID());
roles.addQuery(role.name, thisRole);
roles.setLimit(2);
roles.query();
var userIsAChangeManager = userHasRole(UserID, 'change_management');
var userIsAnAdmin = userHasRole(UserID, 'admin');
//while(roles.hasNext()){
if (roles.next()){
gs.log("work start role check for " + sys_user + " has change_managment role. assigned_to " + current.assigned_to);
if(roles == 'change_management'){
//if (roles.next()){
return true;
}
gs.log("work start role check for " + sys_user + " has admin role. assigned_to " + current.assigned_to);
if(roles == 'admin'){
return true;
}
}
}
return false;
}
All Logs:
org.mozilla.javascript.EcmaError: "role" is not defined. |
Caused by error in <refname> at line 18
15: var roles = new GlideRecord('sys_user_has_role');
16: //var UserID = gs.getUserID();
17: roles.addQuery('user', gs.getUserID());
==> 18: roles.addQuery(role.name, thisRole);
19: roles.setLimit(2);
20: roles.query();
21: var userIsAChangeManager = userHasRole(UserID, 'change_management');
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2016 11:57 AM
lol actually i found the error,
changed role to "roles"
was
roles.addQuery(role.name, thisRole);
changed to
roles.addQuery(roles.name, thisRole);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2016 01:00 PM
Hi Orlando,
I think the original 'role' is correct, because that is the name of the reference field on the 'sys_user_has_role' table.
The line "roles.addQuery(roles.name, thisRole)" looks problematic because 'roles' is being used for two different things, but referencing the same object. This might be called something like a self-referential reality spiral . But I'm curious if you have this working as expected! Are you using this directly in your ACL script or do you have the function as a script include that you're calling?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2016 01:50 PM
Hello Everyone,
So I managed to hash it out. Here is the code:
answer = ifScript();
function ifScript() {
//answer = 'false';
var userID = gs.getUserID();
gs.log("work start check for " + userID + " assigned_to " + current.assigned_to);
if (userID == current.assigned_to) {
return 'true'; }
// query to see if userID has role change management, if so return true
var cm_role = new GlideRecord('sys_user_role');
cm_role.addQuery('name', "change_management");
cm_role.query();
while (cm_role.next()) {
var roles = new GlideRecord('sys_user_has_role');
roles.addQuery('user', userID);
roles.addQuery('role', cm_role.sys_id);
roles.query();
while (roles.next()){
gs.log("work start role check for " + userID + " has role " + cm_role.name);
return true;
}
}
gs.log("end of work start role check for " + userID + ", no writing allowed" );
return false;
}
The errors were as follows:
- I had to only use one "answer". In one variation of the code I had two answers, I could piggy back of the first answer argument.
- turns out that sys_user_has_role doesnt have a table named "name". This is why I had to query the name from the sys_user_role table while grabbing the sys_id of the name of the sys_user_role and populate that in line 20 of the code.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2019 03:25 PM
This is huge! Thank you for posting the update.