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.

getMyGroups gets parent groups as well

Miriam Berg
Kilo Guru

Hi,

The built-in function getMyGroups also returns all parent groups of the groups that you are in.

To only return the specific groups that you are in, create a new script include like so:

Name: getMySpecificGroups

Client Callable: true

Script:

function getMySpecificGroups(){

  var gr = new GlideRecord('sys_user_grmember');

  gr.addQuery("user", gs.getUserID());

  gr.addQuery("group.active", true);

  gr.query();

  var array = [];

  while (gr.next()) {

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

  }

  return array;

}

You can then user this script from for example a module filter with a condition:

Assignment Group                   Is                   javascript:getMySpecificGroups()

Best Regards,

Miriam

3 REPLIES 3

Diane7
Kilo Explorer

Hi Mariam - this does not seem to be working for me; whenever I try to add the script include in the filter area, it is showing "assignment group=(empty) as it is not recognizing the javascript:getMySpecificGroups. It seems to be thinking it is an invalid group name vs running the script. Do you know how to work around this? I do not want parent groups displaying as it clutters up people's work queues.



Thanks,
Diane


Hi Diane,



Can you please in this way and see if it helps:



var getMySpecificGroups = Class.create();


getMySpecificGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getGroups : function(){  


var gr = new GlideRecord('sys_user_grmember');  


gr.addQuery("user", gs.getUserID());  


gr.addQuery("group.active", true);  


gr.query();  


var array = [];  


while (gr.next()) {  


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


}  


return array;  


},


type: 'getMySpecificGroups'


});



In condition, please use below and see if this helps.


javascript:new getMySpecificGroups().getGroups()


Hi Diane,



Did you include the parentheses after?



javascript:getMySpecificGroups()



BR /Miriam