Catalog UI policy g_user.hasRoles()

mrswann
Kilo Guru

I want certain variables in record producer hidden when the user has a role.

tried client UI policy , advanced view and run script but both when true and when false get same result for both roles users and non roled users...

Function onCondition() {

g_user.hasRoles();

}

then when set the action against the variable to not be visible

separately but similar issue

i also tried using g_user to populate the variables with default values and seems difficult to use so going to resort to having different record producers to different users , then tied to templates as the gs.user object seems more dependable but this doesn't seem great from a maintenance POV ?

i have read all I can find on various articles and other blogs and cheat sheets this just seems like it's not the user object itself but the way I'm using it !!?

thsnks for reading

7 REPLIES 7

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi,



What is the exact req. Can you please provide further info so that we can provide solution here.


Hi mrs:



hasRole, typically works on the glide system. and g_user typically only user property, name, userID, firstName, and lastName.   Like Pradeep says, I would also like to know exactly what are you trying achieve by your UI policy.



thanks


Danny


Steve McCarty
Mega Guru

The scripts in the UI Policy are actions that are applied when the conditions are true or false.   They aren't for scripting the conditions themselves.   If you need to hide fields if a user has roles then you should probably look at creating a Catalog Client Script that runs onLoad for the record producer.   That script can use the g_user.hasRoles() function to determine if the current user has roles and if so, you can use g_form.setVisible('variable_name',false); to hide the variables you want hidden or g_form.setValue('variable_name','value'); to set default values for fields.   Be aware that the setVisible function doesn't redraw the form the way the UI Policy does, so there will be a blank space where the variable should be even though it isn't visble.  



You could use a combination of the onLoad script to set a true/false variable if the user has a role and a UI Policy to hide the variables you want hidden if that variable is true.



-Steve


Brian Dailey1
Kilo Sage

Hi mrswan,



You can still use a UI Policy, just leave the condition blank (will always be true) and check for the user roles within your "Execute if true" script.


  1. Clear any conditions that may be set on the policy, so it will always be true/always run.
  2. On the Script tab, check "Run script"
  3. In the "Execute if true" script:


function onCondition() {


        if(g_user.hasRoles()){


                  g_form.setMandatory('your_field_name', false); //You cannot hide your field if it is set to be mandatory


                  g_form.setVisible('your_field_name', false);


        }


        else{


                  g_form.setMandatory('your_field_name', true); //Revert back to mandatory, if that is how you want it


                                                g_form.setVisible('your_field_name', true);


        }


}




That should work for the purpose you stated and still be able to employ UI Policy.




Thanks,


-Brian