Reference qualifier to limit assignment groups field

joshuamayes
Giga Expert

Okay so I am trying to write a script include to use as a reference qualifier.   The idea is that when assigning an incident to a group, only groups that have the ITIL role should be included in the list to choose from.

I found a script here that does pretty much exactly as I want with the assigned_to field.   But I'm really really struggling with how to modify it for the assignment group.

Here's what I have so far:

function u_getGroupsInRole(roleList) { //declare function to be called and expected variable

  var roleListIds;

  if (roleList) {

          roleListIds = getRoleListIds(roleList);   //use the function defined below to get the sysIDs of the role entered into the variable

  }

  var groups = {};

    var gr = new GlideRecord('sys_group_has_role');

    if (roleListIds) {

          gr.addQuery('role', roleListIds);

    }

  gr.query();

    while (gr.next()) {

          groups[gr.group.toString()] = true;

    }

      var ids = [];

    for (var id in groups)

          ids.push(id);

    return ids;

}

// get sys_id's for the named roles

function getRoleListIds(roleList) {

    var roleIds = [];

    var grRole = new GlideRecord('sys_user_role');

    grRole.addQuery('name','IN',roleList);

    grRole.query();

    while (grRole.next()) {

          ids.push(grRole.sys_id.toString());

    }

    return roleIds;

}

And in the reference qualifier field for assignment_group i have the following:

javascript: u_getGroupsInRole('itil')

However, when I click the search button on the assignment group I'm still getting every group in my instance.

What am I doing wrong?

Bonus question:

How is "while (gr.next()) {groups[gr.group.toString()] = true;" supposed to populate groups with an array of stings?   Shouldn't it be something like "while (gr.next()){ groups=gr.group.toString() }; "   Why is "= true;" at the end?   Shouldn't it be something like "while (gr.next()) = true {" ?  

Extra bonus question:

I'm new to javascript (obviously) but how am I able to call a function that further down in the code?   shouldn't I have to call a function after the function has actually been declared?

1 ACCEPTED SOLUTION

HV1
Mega Guru

Try this:



  1. function u_getGroupsInRole(roleList) { //declare function to be called and expected variable  
  2.   var groups = [];  
  3.     var gr = new GlideRecord('sys_group_has_role');  
  4.     if (roleList) {  
  5.           gr.addQuery('role.name', 'IN', roleList);  
  6.     }  
  7.   gr.query();  
  8.     while (gr.next()) {  
  9.           groups.push(gr.group.toString());  
  10.     }
  11.     var str = "";
  12.     if(groups.length>-1){
  13.             str = "sys_idIN"+groups.toString();
  14.     }
  15.     return str;  
  16. }


Your reference qualifier call will be


                      javascript: u_getGroupsInRole('itil,admin');  



View solution in original post

6 REPLIES 6

Abhinay Erra
Giga Sage

Use this in the script include



function u_getGroupsInRole(roleList) { //declare function to be called and expected variable  


var arr=[];


  var gr= new GlideRecord('sys_group_has_role');


gr.addQuery('role.name',roleList);


gr.query();


while(gr.next()){


arr.push(gr.getValue('group'));


}  


return 'sys_idIN'+arr.join();


}



now in ref qualifier use this


javascript: u_getGroupsInRole('itil');


I found this to work when passed one role, but a list of roles caused the reference qualifier to return nothing.


HV1
Mega Guru

Try this:



  1. function u_getGroupsInRole(roleList) { //declare function to be called and expected variable  
  2.   var groups = [];  
  3.     var gr = new GlideRecord('sys_group_has_role');  
  4.     if (roleList) {  
  5.           gr.addQuery('role.name', 'IN', roleList);  
  6.     }  
  7.   gr.query();  
  8.     while (gr.next()) {  
  9.           groups.push(gr.group.toString());  
  10.     }
  11.     var str = "";
  12.     if(groups.length>-1){
  13.             str = "sys_idIN"+groups.toString();
  14.     }
  15.     return str;  
  16. }


Your reference qualifier call will be


                      javascript: u_getGroupsInRole('itil,admin');  



This actually worked perfectly.   Makes me wonder why the script in the reference qualifiers documentation is so complicated.



I'm actually able to understand how this script works except for the "sys_idIN" part.