Need help troubleshooting client callable script include

neil_b
Tera Guru

I have condition on the Menu Item set to the following:

gs.getSession().isLoggedIn() && new checkIfGroupMember().verifyMembership();

The intent of this condition is to check if the user is Logged In and is part of a specific group.

 

Here is the client callable script include:

var checkIfGroupMember = Class.create();
checkIfGroupMember.prototype = {
initialize: function() {},

verifyMembership: function() {
if (gs.getUser().isMemberOf('group_name1') || gs.getUser().isMemberOf('group_name2') || gs.getUser().isMemberOf('group_name3'))
return true;
}, else: {
return: false,
},
    type: 'checkIfGroupMember'
};

 

This is not working, as any user regardless of group can see the dashboard menu item. Can someone help me where I have gone wrong? I don't know if my script include is incorrect or if my condition is incorrect. 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

You might need to pass the current user as an argument in the Script Include.  Where exactly are you using this?  Is your Script Include in the same scope as whatever is calling it?  Your Script Include is not truly Client callable as the extended AjaxProcessor was not added to the beginning, which happens when the box is clicked after the script exists, but it doesn't need to be Client callable in this case - that's only necessary for GlideAjax calls from a client script.

 

Your if statement is not formatted correctly.  Change it to:

if (gs.getUser().isMemberOf('group_name1') || gs.getUser().isMemberOf('group_name2') || gs.getUser().isMemberOf('group_name3')) {
    return true;
} else {
    return false;
},

You may need to add some log lines to confirm this is running, based on the call in the condition, and confirm the value of gs.getUser().  Do you really need the first part of the Condition - is it possible for users in your environment to not be logged in, yet still be members of groups?

View solution in original post

20 REPLIES 20

That makes sense.  First, since this is Portal, try a couple of easy things.  Specify the scope in the script call

gs.getSession().isLoggedIn() && new global.checkIfGroupMember().verifyMembership();

Next, make sure you clear cache on the user you are testing with after making any change to the menus or scripts (cache.do in the URL after service-now.com/)

 

If it's still not working, add some logs to see if the script is running

var checkIfGroupMember = Class.create();
checkIfGroupMember.prototype = {
    initialize: function() {},

    verifyMembership: function() {
        gs.info('SI running');
	    if (gs.getUser().isMemberOf('group_name1') || gs.getUser().isMemberOf('group_name2') || gs.getUser().isMemberOf('group_name3')) {
            gs.info('SI inside if');
		    return true;
        } else {
            gs.info('SI inside else');
		    return false;
        }
    },

    type: 'checkIfGroupMember',
};

Check the System log filtering on message start with SI after testing.

 

You could also try to do this all in the condition, unless you run into a character limit.  Try with one group for starters.

gs.getSession().isLoggedIn() && gs.getUser().isMemberOf('group name 1')

Thanks @Brad Bowman  I tried to indicate the global scope in the condition and that didn't fix it.

 

I tried to add the gs.info messages where you specified but nothing showed up in the log.

 

I tried to set the condition of the menu item to what you suggested, and it works perfectly for each group. The problem though, is that there are ultimately 7-8 groups that I need to include, and this condition field has a limit to the number of characters, which is why I am trying to accomplish this with a script include as I can realistically only add up to 2-3 groups before the character limit is reached. 

 

gs.getSession().isLoggedIn() && gs.getUser().isMemberOf('group name 1')

 

 

Considering that there are no log messages being captured for "SI", the issue must still lie with the script or my condition.

I got a chance to test a mock-up of this in my PDI, to make sure it works as I thought it should, and in short - it does. Here's what I did so you can see where yours might be different, or maybe try this simplified example. Something could be different jumping to pa dashboard...

 

I don't have many portal menus, so I modified the SP Config Menu (Service Portal Configuration in the left nav).  On the Branding Editor menu, I changed the Condition from the same .hasRole that the other menu items have to:

BradBowman_0-1732662859418.png

gs.getSession().isLoggedIn() && new global.refQualUtils().testMe();

Here's my simple Script Include, also in the Global scope, not Client callable:

var refQualUtils = Class.create();
refQualUtils.prototype = {
    initialize: function() {},

    testMe: function() {
        gs.info('BKB SI testMe function running');
        return false;
    },

    type: 'refQualUtils'
};

I wanted to return false so that I would see when launching the page if it worked.  I cleared cache for good measure, then upon logging back in navigated to Service Portal Configuration.  The Branding Editor menu was hidden from the top menu.  I checked the System logs and saw my BKB SI message.  Make sure you have only one Script Include record named checkIfGroupMember.

 

 

Hi @Brad Bowman  I truly do appreciate you trying to help me out. 

 

I have simplified my script include, as you have for simplicity's sake, to test for it being hidden and I still see the dashboard menu item and I'm not seeing my info message in the logs.

 

 

 

// Here is the condition in the menu item page
// gs.getSession().isLoggedIn() && new global.CheckIfGroupMember().verifyMembership();

var CheckIfGroupMember = Class.create();
CheckIfGroupMember.prototype = {
    initialize: function() {},
		
    verifyMembership: function() {
          gs.info('NB SI verifyMembership function running');
          return false;     
    },
    type: 'CheckIfGroupMember'
};

 

 

 

However, I will note, that I performed the exact same steps in my PDI and it works as expected in my PDI. I even confirmed I see my info message in the logs in my PDI. I'm not sure why in the instance that I am in, it isn't working. I thought maybe it would be a cross-scope issue but I don't believe so. The service portal page, the menus, the menu items, and the script includes, are all in the same application scope; they are not in global. 

If they are not in global, then you shouldn't call the script include with global.  Try back to without anything in your simplified script, then try adding the application it's in (the scope field on the application record.  For example, Employee Center is sn_ex_sp).  Make sure it's accessible from all application scopes just in case, and that the user you are testing with has the role listed in the Access Control Related List, if there is one.