
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2021 05:00 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2021 12:03 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2024 01:14 AM
Thank you very much for your answer.
I also solved the same problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2021 05:21 AM
Hi,
You can use ArrayUtil class contains method to check if array contains any element.
It's OOB class and efficient
Update with changes in bold
var checkedManagers = new Array();
function isApprovingManager(userID) {
checkedManagers.push(userID);
//DEBUG CODE
var arrayUtil = global.ArrayUtil();
gs.addInfoMessage(userID + " added to checkedManagers: " + checkedManagers.toString());
if (arrayUtil.contains(checkedManagers,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;
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader