Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to hide widget based on user role

tricial
Giga Contributor

So I have searched the community near and far and have not been able to find a solution.

I have cloned the ICON Link widget and need to now set the visibility of this based on the logged in user having a specific role.

If they have the role show the widget, if they don't have the role don't show the widget.

It seems simple enough, but I am new to ng-if angular and the data. options.schema stuff

I really could use some help with some sample code on how to achieve this.

Thank you very much

5 REPLIES 5

tricial
Giga Contributor

What I have tried is to create option


always_show (boolean)



Server Script:


(function(){


var gr = $sp.getInstanceRecord();


data.href = $sp.getMenuHREF(gr);


data.target = options.target || "";



var lUser = gs.getUserID();



var gRole = new GlideRecord('sys_user_has_role');


gRole.addQuery('role', 'sp_password_services');


gRole.addQuery('user', 'lUser');


gRole.query();


if(gRole.next()){


options.always_show = true;


}



})();



Added to the top <div in html the following:


<div ng-if="{{::options.always_show == 'true'}}">



It's not working 😞


Jaspal Singh
Mega Patron
Mega Patron

Hi Patricia,



Easiest option is to open up the widget form & specify the role as marked below.


find_real_file.png



Thanks,


Jaspal Singh



Hit Like or Correct on the impact of response.


Hi Jaspal,



I tried that and when I impersonate a user with NO ROLE they still see the widget on the portal


So I went back to the widget on the form and the role was cleared out


I set it to


find_real_file.png



When I go back to look at it the roles are gone


tricial
Giga Contributor

Jaspal,



I created a new widget from the copy icon link widget, and set the role on this new widget.


It seems to have worked.



I think the problem is that I had multiple instances of the copy of icon link widget on the page and it was not working.


At least I am assuming thats what it was.


I am going to test this theory by trying to add a role again to the copy icon link widget, I have 2 instances of this on the portal.


If it does not hide it based on role then I will venture to guess that is the case.



Will let you know what I find out.



Thank you for your reply


Tricia