Iterating through list of groups in UI page to check membership

logan2
Mega Contributor

Hi everyone. We are currently using a UI page with a script to iterate through a list of groups and then checking membership of those groups(in the form of sys_ids). The iteration IS working just fine. Just when I pass the value to the isMemberOf function is where it does not want to work. I have tried countless different ways to figure this out with no luck.

Here is the code snippet below:

<j:set var="jvar_groups" value="${gr.assignment_group.toString().split(',')}" />

  <j:forEach var="jvar_group" items="${jvar_groups}">

            <div>${jvar_group}</div>

            <j:if test="${gs.getUser().isMemberOf('${jvar_group}')}">

                      <g:macro_invoke macro="x_sample_macro" />

                      <j:break />

                      </j:if>

  </j:forEach>

I have also tried manually wrapping the string with quotes and passing that using escape characters and such and even though it is printing out fine to the page with quotes, it isn't getting any results in the isMemberOf because it is not returning anything. If I hardcode one of the sys_ids in, though, it works fine. Any advice/guidance would be appreciated.

Thanks, all.

4 REPLIES 4

logan2
Mega Contributor

Have tried .isMemberOf("${jvar_group}"), .isMemberOf(${jvar_group}) and several others


I tried a couple different things and similarly couldn't get it to work.



Could you explain a little more about what you're doing (and maybe include the whole UI page)? Maybe there is another method for accomplishing this that might work better.


So below is a simplified version of our code without unneccessary classes and code. What we basically want to do,is to only show the macro when the user is either assigned to it or in the assignment groups. The assigned to part works, so I removed that code from below to make it more legible.



  <g:evaluate>


            var gr = new GlideRecord('my_table');


            gr.orderBy("due_date");


            gr.query();


  </g:evaluate>


  <div class="container">


            <div class="col-xs-12">


                      <div class="row" style="width:100%">


                                <j:while test="${gr.next()}">


                                          <j:choose>


                                                    <j:when test="${gr.assigned_to.toString().indexOf(gs.getUser().getId()) == -1}">


                                                              <j:set var="jvar_groups" value="${gr.assignment_group.toString().split(',')}" />


                                                              <j:forEach var="jvar_group" items="${jvar_groups}">


                                                                        <j:if test="gs.getUser().isMemberOf('${jvar_group}')">


                                                                                  <g:macro_invoke macro="sample_macro" />


                                                                                  <j:break />


                                                                        </j:if>


                                                              </j:forEach>


                                                      <j:otherwise>


                                                              <g:macro_invoke macro="sample_macro" />


                                                      </j:otherwise>


                                          </j:choose>


                                </j:while>


                  </div>


        </div>


</div>


Well, being that I'm not sure exactly what is going on in the jelly, I can try to approach it from the actual glide record. I'm still not 100% what the use case of your scenario is (or what your macro does).



You'll need to change bold references in your query, naturally.



Your query is a little bit more difficult than the standard assignment group because yours can have multiple values. What you were explaining were pretty similar to the out of box dynamic filters like Assigned To (dynamic) Me, or Assignment Group (dynamic) One of my Groups.



//Store the user, and their groups


var dynamicMe = gs.getUserID();


var dynamicGroups = gs.getUser().getMyGroups().toArray();



//Build query against table in question


var gr = new GlideRecord('incident');



//Build query string, I'm starting with active is true, assigned to me, then adding the rest of your or conditions


var queryString = 'active=true^assigned_to=' + dynamicMe + '^OR';



//For each of the users groups, add to the query string the condition that "u_assignment_groups" contains the current index of the group array


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


queryString += 'u_assignment_groupsLIKE' + dynamicGroups[i] + '^OR';


}


//Trim off the trailing '^OR'. If you prefer, you can add it at the start of each for loop except for the first one by adding an if to test for i=0... this seemed quicker


queryString= queryString.substring(0, queryString.length - 3);



//Print the query string for testing purposes (I'm executing this in a background script)


gs.print(queryString);



//Add the encoded query and execute it


gr.addEncodedQuery(queryString);


gr.query();