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

This would work - but you can simplify it a bit like this:



  1. if (mytable.next()) {  
  2.               var groups = mytable.assignment_group.toString(); // Just treat the list as a string, using the 'IN' Operator.
  3.               var memberOf = new GlideRecord('sys_user_grmember');  
  4.               memberOf.addQuery('user', currentUser);  
  5.               memberOf.addQuery('sys_id', 'IN', groups);  
  6.               memberOf.query();  
  7.               if (memberOf.hasNext()) {  
  8.                       return true; // User is a member of one of the groups on the list  
  9.               } else {  
  10.                       return false; // User is not a member  
  11.               }  
  12.               }

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>