- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2023 01:45 PM
Hello I have a workflow that includes a scripted if condition, but it consistently evaluates to false when it shouldn't. The objective of the script is to verify if a list collector of groups contains at least one group with only one member. I've added logs to the script so I can see the right logic is hitting but, the condition always results in false. Am I missing something or does anyone see an issue with the script?
here is the script.
answer = checkGroupsAndReturnResult();
function checkGroupsAndReturnResult() {
var groupsList = current.variables.add_group.getDisplayValue();
var groupNames = groupsList.toString().split(',');
gs.log("Processing groups...");
for (var i = 0; i < groupNames.length; i++) {
var groupName = groupNames[i].trim();
if (groupName.length > 0) {
gs.log("Processing group: " + groupName);
var group = new GlideRecord('sys_user_group');
if (group.get('name', groupName)) {
gs.log("Group found: " + groupName);
// Check if the group has only one member
var membersCount = new GlideAggregate('sys_user_grmember');
membersCount.addQuery('group', group.sys_id);
membersCount.addAggregate('COUNT');
membersCount.query();
if (membersCount.next()) {
var memberCount = membersCount.getAggregate('COUNT');
gs.log("Group " + groupName + " has " + memberCount + " member(s).");
if (memberCount === 1) {
gs.log("Group " + groupName + " has only one member.");
return 'yes'; // Return "yes" if at least one group has one member
}
}
} else {
gs.log("Group not found: " + groupName);
}
}
}
// If we reach this point, it means no group had one member
return 'no';
}
any assistance would be helpful thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2023 12:41 AM - edited ‎10-09-2023 12:42 AM
Hi @Daga579
Give the below script a try...
answer = checkGroupsAndReturnResult(current.variables.add_group.toString());
gs.info(answer);
function checkGroupsAndReturnResult(group_ids) {
var gaMember = new GlideAggregate('sys_user_grmember');
gaMember.addQuery('group', 'IN', group_ids);
gaMember.groupBy('group');
gaMember.addAggregate('COUNT', 'user');
gaMember.query();
while(gaMember.next()){
var total_member = gaMember.getAggregate('COUNT', 'user');
if(total_member === '1'){
gs.info(gaMember.getDisplayValue('group'));
return 'yes';
}
}
return 'no';
}
Let me know if it works for you.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2023 02:27 PM
Hi, what are the results of your debugging? have you logged
current.variables.add_group.getDisplayValue();
to confirm it has a display name and that the display name is relevant to your functionality?
Based on your script I am guessing that the variables content is the string you are after, and it is a string of comma separated group names or sys_ids? In which case you need to drop the displayValue() method and simple read the variable content as a string, then split into an array. If the variable is a list of sys_id's then you should be able to drop the sys_user_group 'get' and simply use the sys_id in your sys_user_grmember query.
Perhaps you could update this thread to clarify your debugging results, so that the community has a better understanding of your issue and is in a better position to review and advise.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2023 07:43 AM
the field is a list collector of groups so I thought it easiest to get the display value of the groups and check it against the sys_user_grmember table. I was able to have the logs message display this which means it is hitting into the right if statement:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2023 12:41 AM - edited ‎10-09-2023 12:42 AM
Hi @Daga579
Give the below script a try...
answer = checkGroupsAndReturnResult(current.variables.add_group.toString());
gs.info(answer);
function checkGroupsAndReturnResult(group_ids) {
var gaMember = new GlideAggregate('sys_user_grmember');
gaMember.addQuery('group', 'IN', group_ids);
gaMember.groupBy('group');
gaMember.addAggregate('COUNT', 'user');
gaMember.query();
while(gaMember.next()){
var total_member = gaMember.getAggregate('COUNT', 'user');
if(total_member === '1'){
gs.info(gaMember.getDisplayValue('group'));
return 'yes';
}
}
return 'no';
}
Let me know if it works for you.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2023 07:37 AM
That worked thank you, much more streamlined