Workflow IF Script not worrking correctly

Daga579
Tera Contributor

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.

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

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

View solution in original post

4 REPLIES 4

Tony Chatfield1
Kilo Patron

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.

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: Screenshot 2023-03-01 080220.png

 

Tai Vu
Kilo Patron
Kilo Patron

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

Daga579
Tera Contributor

That worked thank you, much more streamlined