GlideRecord compare list to a list(user groups)

logan2
Mega Contributor

I am working on a project where we want a specific function to happen. Either a ticket is assigned to a user, or it is assigned to user group(s). The group in the table references the User Groups table and is a LIST date type. I want to check where user is in the list of assigned_to users, or where the user is in any of the groups.

So if the ticket was only assigned to one user group, then this would be fairly simple. But it is effectively a list compared to a list. Any guidance would be appreciated. This is on a custom table in a custom application.

var gr = new GlideRecord('');

gr.addQuery('assigned_to', 'CONTAINS', gs.getUser().getID());

gr.query();

I know to add an or condition but dont really understand what to put in the condition.

gr.addQuery('assigned_to', 'CONTAINS', gs.getUser().getID()).addOrCondition('assignment_group', ' ', ' ');

Any help would be appreciated. Thanks.

1 ACCEPTED SOLUTION

This was my solution:



  1. <j:set var="jvar_groups" value="${gr.assignment_group.toString().split(',')}" />  
  2.   <j:forEach var="jvar_group" items="${jvar_groups}">  
  3.             <div>${jvar_group}</div>  
  4.             <j:if test="${gs.getUser().isMemberOf('${jvar_group}')}">  
  5.                       <g:macro_invoke macro="x_sample_macro" />  
  6.                       <j:break />  
  7.                       </j:if>  
  8.  
  9.  
  10.   </j:forEach>

View solution in original post

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Logan,



Do you want to check if the caller is part of any group?..Can you please elaborate further your req with a example so that we can help.


Yeah no problem. So in my custom table, the row assignment_group is a reference to the sys_user_group table with data type of list. I want to see if out of all the groups that a user is in, if that user has a group that is in the list on the table. The user in question would be the user object found by gs.getUser().


Hi Logan,



You can query against the table sys_user_grmember by passing the sys_id of the user as a addQuery to return true or false.


Please let me know if you have any questions.


Hi Logan,



Building on Pradeep's suggestion, I think I would do something like this (if I'm following you correctly). For fun, I made a new table called MyTable and made a list reference to the Groups table called assignment_group on it.



isMember('MYT0001001');



function isMember(ticketNumber) {


      var currentUser = gs.getUser().getID();


      var mytable = new GlideRecord('u_mytable'); // Custom table called MyTable that has a reference list to sys_user_groups


      mytable.addQuery('number', ticketNumber);


      mytable.query();



      if (mytable.next()) {


              var groups = mytable.assignment_group.split(","); // Split the list string of sys_ids into an array


              var encodedQuery = ""; // Build encoded query string to use on sys_user_grmember table


              for (i = 0; i < groups.length; i++) {


                      if (i == 0) {


                              encodedQuery += 'group=' + groups[i];


                      } else {


                              encodedQuery += '^ORgroup=' + groups[i];


                      }


              }



              var memberOf = new GlideRecord('sys_user_grmember');


              memberOf.addQuery('user', currentUser);


              memberOf.addEncodedQuery(encodedQuery);


              memberOf.query();


              if (memberOf.hasNext()) {


                      return true; // User is a member of one of the groups on the list


              } else {


                      return false; // User is not a member


              }


      }


}




I haven't tested any of this, but hopefully it helps some what.