get user role in UI Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2017 01:37 PM
Hi, I'm trying to create a UI page that has certain fields readonly based on role. I have the following code in my client script, but I'm getting errors in my console saying gs is not defined and $(...).ready is not a function. Can someone help me debug this? Am I even on the right track?
var roles = [];
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', gs.getUserID()) ;
gr.query();
while(gr.next()) {
roles.push({
role : gr.getDisplayValue('role') //Will give the sys_id of the roles
});
}
if (roles.indexOf('admin')) {
document.getElementById('signature').readonly = true;
document.getElementById('signature').style.backgroundColor = '#ddd';
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2017 01:48 PM
Hello David,
Replace gs.getUserID() with g_user.userID for client side operation.
GlideUser (g user) - ServiceNow Wiki

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2017 01:49 PM
Also getDisplayValue() only works on server side. To get the display value, you need to do this via client script + GlideAjax approach. More info here.
http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2017 01:53 PM
This is helpful but doesn't address the bad practice of using GlideRecord in a client side script.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2017 02:03 PM
Thanks Fred for the update. I missed that point.
@David, please refer section 3 for more info related to best practice for doing a server side query from the client side.
Client Script Best Practices - ServiceNow Wiki