Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

get user role in UI Page

davilu
Mega Sage

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';

}

20 REPLIES 20

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello David,



Replace gs.getUserID() with g_user.userID for client side operation.


GlideUser (g user) - ServiceNow Wiki


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


This is helpful but doesn't address the bad practice of using GlideRecord in a client side script.


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