- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 04:51 AM
Hi All,
Below is the script i written but it's not working. every time it's moving to else condition instead of if condition. can you help me on this
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 05:47 AM
Hi @Emp 53
This is because you are not returning the array in your function. Try the below script.
var userId = '123';
var reqQuery = "";
function getUserGroups(userSysId) {
var groupIds = [];
var groupMemGr = new GlideRecord('sys_user_grmember');
groupMemGr.addQuery('user.user_name', userId);
groupMemGr.query();
while (groupMemGr.next()) {
groupIds.push(groupMemGr.getValue('group'));
}
return groupIds;
}
if (global.JSUtil.notNil(userId)) {
var userGr = new GlideRecord('sys_user');
if (!userGr.get('user_name', userId)) {
sendError('user id not defined ' + userId);
}
var groupIds = getUserGroups(userGr.getUniqueValue());
if (groupIds.length > 0)
reqQuery += '^assignment_groupIN' + groupIds.toString() + '^ORrequested_for.user_name=' + userId;
else
reqQuery += '^requested_for.user_name=' + userId;
}
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 05:47 AM
Hi @Emp 53
This is because you are not returning the array in your function. Try the below script.
var userId = '123';
var reqQuery = "";
function getUserGroups(userSysId) {
var groupIds = [];
var groupMemGr = new GlideRecord('sys_user_grmember');
groupMemGr.addQuery('user.user_name', userId);
groupMemGr.query();
while (groupMemGr.next()) {
groupIds.push(groupMemGr.getValue('group'));
}
return groupIds;
}
if (global.JSUtil.notNil(userId)) {
var userGr = new GlideRecord('sys_user');
if (!userGr.get('user_name', userId)) {
sendError('user id not defined ' + userId);
}
var groupIds = getUserGroups(userGr.getUniqueValue());
if (groupIds.length > 0)
reqQuery += '^assignment_groupIN' + groupIds.toString() + '^ORrequested_for.user_name=' + userId;
else
reqQuery += '^requested_for.user_name=' + userId;
}
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh