Hiding Catalog Item Variables based on Group Membership

robpickering
ServiceNow Employee
ServiceNow Employee

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.

1 ACCEPTED SOLUTION

Nestor2
Giga Contributor

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


View solution in original post

8 REPLIES 8

Totally agree, I have those all taken care of for 'itil' users.   Works like a charm.


It's got to be relatively easy, as you can show/hide whole Catalog Items based on Group Membership, but the UI doesn't allow that to extend easily to individual variables on a Catalog Item.



I've even gotten it to work on the Catalog Item Form, but not on the CMS Catalog Item itself, which leads me to think I've got my Display Business Rule on the wrong table (currently on sc_cat_item).   I don't see it execute when I Debug Business Rules.


Nestor2
Giga Contributor

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


robpickering
ServiceNow Employee
ServiceNow Employee

Just as an aside...Nestor's answer works, but ultimately, the solution that is the most portable would be:


  1. Create roles and assign them to the Groups in question.
  2. Utilize the built-in functionality to "Exclude" Catalog Variables based on Role (that Jim Coyne mentioned).   This is fully supported by ServiceNow and would survive future upgrades.


Thanks for the answer Nestor!


sharansomanna
Kilo Contributor

In which table is the display business rule written? @Nestor 

Does not seem to work for me