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

Could you post a screenshot of your UI Page configuration?



Below is the UI Page I configured in my personal developer instance (Jakarta).   When I call this UI page from a UI action, I get the two alerts shown.



find_real_file.png



find_real_file.png



find_real_file.png


Hi fred, thanks for helping me out.   Sorry, how did you get the alerts from a UI Action?   My code for the UI Page is below:



find_real_file.png



When I "Try It", I see this:



find_real_file.png


I have admin role so theoretically the HR Signature line should be grayed out and readonly, with dave in the input.   The errors noted in my previous post are in my browser console.   Thanks again.


Happy to help, and sorry if I'm not explaining things clearly.



I tested the "Try It" button in my personal developer instance and didn't get the alerts - I suspect that button only displays the HTML section but does not execute the client scripts.   To get my alerts, I set up an UI action on the incident table just as a test.   The code I'm using in my UI action is the following:



function test222() {


        var dialog;


        try {


                  dialog = new GlideModal('test222');


        } catch(e) {


                  dialog = new GlideDialogWindow('test222');


        }



        dialog.setTitle('test');


        dialog.setSize(750,300);


        dialog.render();


}


}



This UI action is client callable and calls the test222() function onClick.   Using the UI action then calls the UI Page and executes both the HTML and client script.


Dave Smith1
ServiceNow Employee
ServiceNow Employee

David Lu wrote:



certain fields readonly based on role.          


Any reasons an ACL can't be used here?   That's pretty much the standard solution to your problem definition.


Hey Dave, I looked into this briefly, but am a bit confused of how to apply ACL's on specific fields to specific UI Pages.   Is there any material on this?



Below is the ACL page I see, but where would we define which UI Page this rule applies to?   I assume if we have specific fields that need to be readonly depending on role, that would have to be scripted as advanced right?



Thanks.



find_real_file.png