"gs.getUser().isMemberOf('Group_name') " is not working in UI script

payal1
Kilo Explorer

Hi All,

I want to show i icon in list only for one group members but it is not working. I'm writing below UI script , i icon in list should be visible to "SCS Leaderboard Group" group members and it should be hidden to others,

(function() {
    addAfterPageLoadedEvent(function() {
        if (window.location.href.indexOf('u_dashboard_metrics_new_list') != -1) {
            if (gs.getUser().isMemberOf('SCS Leaderboard Group')) {
                // if (g_user.isMemberOf('SCS Leaderboard Group')) {
                //         $j('a[class="btn btn-icon table-btn-lg icon-info list_popup"').each(function() {
                //           $j(this).show();
                //     });
            } else {
                //hideRefIcons();
                $j('a[class="btn btn-icon table-btn-lg icon-info list_popup"').each(function() {
                    $j(this).css("display", "none");
                });
            }
        }
    });
})();
9 REPLIES 9

Harsh Vardhan
Giga Patron

UI script accept client side method , gs.getUser() works on server side. 

 

either you can use glide ajax to get the gs.getUser().isMemberOf('SCS Leaderboard Group') value 

Prasant Kumar 1
Kilo Sage

Hi,

Basically the Ui Script only works on client side and "gs.getUser().isMemberOf('group_name')" , this particular script is a server side script.

So Instead you can use:- 

There are two options.

1st one is to create a client callable script include and call the script from client script using glideajax.

The other option is create a new role and assign to <SCS Leaderboard Group>.

Then in client script you can check if (g_user.hasRoleExactly('your_new_role'))

 

Or 

You can follow the below link:-

https://community.servicenow.com/community?id=community_question&sys_id=18e01ba9dbdcdbc01dcaf3231f96...

 

If i was able to solve your query, please mark my answer correct and helpful.

Thanks & Regards

Prasant kumar sahu

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Payal,

it won't work since UI script is client side and gs object is server side

2 approaches you can have

1) create custom role and give that role to that group so that all group members get that role and update script as below

if(g_user.hasRoleExactly('new_role')){

// hide

}

2) Use GlideAjax and Script Include and then check whether user is member of that group

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

I am trying to get the currently logged-in user's name in my script include. And for that, I am using gs.getUserName() function.

But it always returns system although another or say any user is logged in.

Below is the code:

    var userName = gs.getUserName();  // It returns system

    var userSysID = gs.getUserID();  // It also returns system

 

So, is there any way I can get proper results on the server side using these functions.?

It will be a great help.

Thanks in advance.

- Kaushal