- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2014 01:04 PM
I'm trying to hide certain variables on a Catalog Item (actually part of a Variable Set) based on Group Membership (and roles).
My use case is basically hiding a checkbox if the user does not have either the 'itil' role, or is a member of the 'Human Resources' group (no role).
I don't seem to be able to do this via a UI Policy, because I only want to show/hide a single variable of a variable set.
So, I settled on a Display Business Rule and a Client Script combo in order to achieve it (open to better solutions); however, I can't get this combination to work.
Display Business Rule:
userIsMemberHR(); function userIsMemberHR() { g_scratchpad.isMemberOfHR = gs.getUser().isMemberOf('Human Resources'); }
Client Script:
function onLoad() { var itil_usr = g_user.hasRole('itil'); if ((!itil_usr) && (!g_scratchpad.isMemberOfHR)) { // 'itil' role or 'Human Resources' group var val = g_form.getValue("variables.offboard_immediately"); g_form.setDisplay('variables.offboard_immediately',false); } }
Before adding the group membership portion, the role membership was working fine with just the Client Script.
Appreciate your assistance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2016 07:33 AM
Hello,
I was looking for info on this and tried your example, this works in geneva P7.
This is the onDisplay BR:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
g_scratchpad.isMemberOfITSAPECC = gs.getUser().isMemberOf('IT SAP ECC');
g_scratchpad.myTest = "hola";
})(current, previous);
And this is the CS:
function onLoad() {
//Type appropriate comment here, and begin script below
var myVariablesAreReadOnly = true;
if((g_scratchpad.isMemberOfITSAPECC && g_form.getValue("state") == 9) || g_user.hasRole("admin")){
myVariablesAreReadOnly = false;
}
g_form.setReadOnly("variables.u_sap_short_description", myVariablesAreReadOnly);
g_form.setReadOnly("variables.u_sap_testers_approvals", myVariablesAreReadOnly);
g_form.setReadOnly("variables.u_sap_business_approvals", myVariablesAreReadOnly);
}
Just in case anyone else is trying this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2014 03:14 PM
Hi Rob
I think the complexity of your logic is due to the fact that you cannot determine what groups the user belongs to on the client side. Have a look at this page:
http://wiki.servicenow.com/index.php?title=Session_Client_Data
Basically this page explains you how to store 'custom' information in g_user variable. You can use this functionality to store a list of the groups that the user belongs to when the user logs in, and then read it on the client side.
In order to store the list of the groups when a user logs in, you will need to write a script action
http://wiki.servicenow.com/index.php?title=Script_Actions
Script Action runs a defined script (server-side) when an event occurs. Create a script action that is associated with session.established event, and then write a script to store the group list into your g_user variable using Session Client Data.
Note: session.established event is fired when a user logs in.
This script action will need to be Synchronous = true, but I couldn't find a proper way to do this without maint role. So I just ran a background script to enforce it.
This solution may be an overkill for what you're trying to achieve. But it worked well for me, because I often needed to access the list of groups that the current user belongs to on the client side, and I needed an easy and simple way to access to it (without using ajax/scratchpad/etc)
Kevin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2014 03:21 PM
Kevin, I knew I wouldn't be able to determine the groups the user was a memberOf on the client, that's why I figured a Display Business Rule would be able to do it.
Your logic seems valid, if a bit complex, I'll dig into the links you provided, thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2014 03:29 PM
Feel free to ask if you get stuck. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2014 03:48 PM
It's too bad your HR group does not have any roles because you could have used role-based restrictions - Using Service Catalog Variables - ServiceNow Wiki