- 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-07-2016 03:44 PM
You need to iterate through what roles.query() returns, also don't needs quotes around role, but you do around change_management. So I think your code should look like this:
roles.addQuery('user', userID);
roles.addQuery(role, 'change_manager');
roles.query();
while(roles.hasNext()){
if(roles == 'change_manager'){
return true;
}
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2016 05:43 PM
Hello Robbie,
Is there a function associated to this as well?
Thanks for your prompt reply and help on this situation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2016 03:48 PM
This should work:
var grUserRoles = new GlideRecord('sys_user_has_role');
grUserRoles.addQuery('role', 'SYS ID OF ROLE HERE'); // Enter the SYS_ID of the role you want to search for - change_manager within quotes
grUserRoles.addQuery('user',gs.getUserID());
grUserRoles.query();
if(grUserRoles.getRowCount() > 0){
return true;
} else {
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2016 05:58 PM
Hello Top,
I am not sure I am doing this correctly. Please advise. Here is my 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 grUserRoles = new GlideRecord('sys_user_has_role');
grUserRoles.addQuery('role', '77a36ef12bc25200980bc67319da15e5'); // Enter the SYS_ID of the role you want to search for - change_manager within quotes
grUserRoles.addQuery('user',gs.getUserID());
grUserRoles.query();
if(grUserRoles.getRowCount() > 0){
return true;
} else {
return false;
}
}
Thank you for all your help.