UI action condition to check whether the user in the owned by field has role hl_help

anto
Kilo Expert

I want to display help button in incident form only when the user in the owned by field has role hl_help.

the owned by field is in incident table.

I just tried this condition but it doesnt work

current.u_owned_by.role=="hl_help"

Thanks in advance

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage
current.u_owned_by.role=="hl_help"


That won't work as Roles are stored in the many to many table "sys_user_has_role".



Luckily enough ServiceNow provide us with the GlideUser API to do this without needed to query the database directly.


See http://wiki.servicenow.com/index.php?title=Getting_a_User_Object#gsc.tab=0



Line by line:


var glideUser = gs.getUser();


var ownedByUser = glideUser. ­getUserByID( ­current.u_owned_by.user_name);


var hasRole = ownedByUser.hasRole('hl_help');



As Pradeep has suggested, throw this in a script include and call it in your condition field.



Script Include


Script Includes - ServiceNow Wiki



You could then end up with something like



Script Include


UIActionConditionHelper.userHasRole = function(role, user_name) {


  var glideUser = gs.getUser();


  var ownedByUser = glideUser. ­getUserByID(user_name);


  var hasRole = ownedByUser.hasRole(role);


  return hadRole;


};


var UIActionConditionHelper = Class.create();



UIAction Condition


UIActionConditionHelper.userHasRole('hl_help',current.owned_by.user_name);



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

3 REPLIES 3

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Joshwa,



Is the role field populated on the sys_user table. The best way would be to write a script include and then return true or false based on the validation.


Please let me know if you have any questions.


The SN Nerd
Giga Sage
Giga Sage
current.u_owned_by.role=="hl_help"


That won't work as Roles are stored in the many to many table "sys_user_has_role".



Luckily enough ServiceNow provide us with the GlideUser API to do this without needed to query the database directly.


See http://wiki.servicenow.com/index.php?title=Getting_a_User_Object#gsc.tab=0



Line by line:


var glideUser = gs.getUser();


var ownedByUser = glideUser. ­getUserByID( ­current.u_owned_by.user_name);


var hasRole = ownedByUser.hasRole('hl_help');



As Pradeep has suggested, throw this in a script include and call it in your condition field.



Script Include


Script Includes - ServiceNow Wiki



You could then end up with something like



Script Include


UIActionConditionHelper.userHasRole = function(role, user_name) {


  var glideUser = gs.getUser();


  var ownedByUser = glideUser. ­getUserByID(user_name);


  var hasRole = ownedByUser.hasRole(role);


  return hadRole;


};


var UIActionConditionHelper = Class.create();



UIAction Condition


UIActionConditionHelper.userHasRole('hl_help',current.owned_by.user_name);



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thank you Pradeep/Paul.



Great Learning...


Everyday you guys are teaching me something


Thanks for your wonderfull help


Cheers..