array.includes() always return false

Patrik Samuel T
Tera Contributor

Hi Guys,

I am having troubles with following function, which is checking whether user is member of a group and should also prevent looping (check is done elsewhere). Right after I call a function, I am adding userID to array of "checkedManagers", but if I then check if the array contains such userID, result is always false.

Code:

var checkedManagers = new Array();

function isApprovingManager(userID) {
    checkedManagers.push(userID);
//DEBUG CODE
	gs.addInfoMessage(userID + " added to checkedManagers: " + checkedManagers.toString());
	if (checkedManagers.includes(userID))
		{
			gs.addInfoMessage(userID + " LISTED");
		}
	else
		{
			gs.addInfoMessage(userID + " NOT LISTED");
		}
//DEBUG CODE
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('user', userID);
    gr.query();
    while (gr.next()) {
        if (gr.group == 'ddf3d1dedb4f20d0b56645403996196c') //Approving Manager = ddf3d1dedb4f20d0b56645403996196c
        {
            return true;
        }
    }
    return false;
}

Debug may look like:

2e7e51fcdb69470018aa9ee3db9619d8 added to checkedManagers: 00b0d9a1db761f80ba3f92b8db9619cf,c70fd9ecdb254700fd889414db961985,413539c0dbff2b40ba3f92b8db9619c9,0d9f1ae1dbd01410e98c6d6bd39619fb,2e7e51fcdb69470018aa9ee3db9619d8,2e7e51fcdb69470018aa9ee3db9619d8
2e7e51fcdb69470018aa9ee3db9619d8 NOT LISTED

Any ideas why the check fails?

I am quite sure its something stupidly simple, as always...

Thanks,

Patrik

1 ACCEPTED SOLUTION

Log the type of the userID variable you push into the array to make sure it's a string.

gs.info("Typeof userID: " + typeof userID);
checkedManagers.push(userID);

View solution in original post

11 REPLIES 11

Still haven't figured this out and I really have no idea what is wrong.

Even when I change condition as below, it always returns -1.

var managerID = gr.manager;
        workflow.info("userID: " + userID + " managerID: " + managerID);
        workflow.info("checkedManagers: " + checkedManagers.toString());
        if (checkedManagers.indexOf("2e7e51fcdb69470018aa9ee3db9619d8") == -1) {
            workflow.info(managerID + " was NOT yet checked: " + checkedManagers.indexOf(managerID));

Debug output is:

userID: 2e7e51fcdb69470018aa9ee3db9619d8 managerID: 2e7e51fcdb69470018aa9ee3db9619d8	
checkedManagers: 00b0d9a1db761f80ba3f92b8db9619cf,c70fd9ecdb254700fd889414db961985,413539c0dbff2b40ba3f92b8db9619c9,0d9f1ae1dbd01410e98c6d6bd39619fb,2e7e51fcdb69470018aa9ee3db9619d8
2e7e51fcdb69470018aa9ee3db9619d8 was NOT yet checked: -1

 

Any ideas?

Btw, getUniqueValue() was not needed and it was actually returning "undefined".

Change the comparison to this:

if (checkedManagers.indexOf(gr.manager.toString()) == -1) {

gr.manager is an object and checkedManagers is a string array so it wasn't being found in the array.

The problem is, it does not even find hard coded string, as you can see above.

 

Log the type of the userID variable you push into the array to make sure it's a string.

gs.info("Typeof userID: " + typeof userID);
checkedManagers.push(userID);

That was it, I had array of bloody objects! I actually wanted to check the type of userID yesterday already, but then started working on something else and forgot about it...

 

So, I just simply changed:

var managerID = gr.manager;

To:

var managerID = gr.manager.toString();

Thanks for your patience 😉