Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

LOL you did not mention that you are passing multiple roles. Hard work was mine and credit goes to someone else


Hey sorry, didn't mean to offend.



I'm actually comparing the two to determine why one works with multiple roles and the other doesn't, so your efforts have actually been really helpful.   I would have marked both correct but the forum doesn't allow for that.  



The best that I can tell is the gr.addQuery line.   His has x, IN, y where yours is just (x, y).



As it turns out


var u_getGroupsInRole = Class.create();  


u_getGroupsInRole.prototype = {  


      initialize: function() {  


      },  


 


 


create: function(roleList) {  


var arr=[];


  var gr= new GlideRecord('sys_group_has_role');


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


gr.query();


while(gr.next()){


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


}


return 'sys_idIN'+arr.join();


},


 


 


      type: 'u_getGroupsInRole'  


};  



actually works too when I use javascript: new u_getGroupsInRole().create('itil,admin');