Reference Variable Filtered by Group

Laurie Marlowe1
Kilo Sage

Hi,

I have a reference variable called "Project Lead".   I want to only show members of the Physical Security group in this field.   From what I've read it seems a reference qualifier should be used.   After reading some articles I tried entering the following:

Reference qualifier:   javascript:new GetGroupMember().getMember(bb3fc3056f68e10006a8f00dba3ee483)

I'm passing the sys_id of the Physical Security group.

Script Include:

var GetGroupMember= Class.create();

GetGroupMember.prototype = {

getMember : function(bb3fc3056f68e10006a8f00dba3ee483)

{

   

      var user_array = [];

      var getMembers = new GlideRecord('sys_user_grmember');

      getMembers.addQuery('group',bb3fc3056f68e10006a8f00dba3ee483);

      getMembers.query();

      while(getMembers.next())

              {

                user_array.push(getMembers.getValue('users'));

      }

      return 'sys_idIN' + user_array.toString();

}

};

What happens is I get the entire sys_user table unfiltered.   How do I just get the members of the Physical Security group so I can select one of them?

Any help is appreciated, as I am new to scripting.

Thanks,

Laurie

1 ACCEPTED SOLUTION

Please see my other reply as it will correct this situation.


View solution in original post

36 REPLIES 36

Hi There, 

Has anyone had any success using this BR in a custom application scope? I am trying to use it in a custom app I created, but I'm not having any luck. When I add the BR and the reference qualifier, my reference list still shows all of the sys_users unfiltered

 

I created a new Business Rule - I have the BR set to run "before" on insert and update.

 find_real_file.png

 

Under Advanced, I added your above script:

function getGroupedUsers(queryCondition, groupList) {
var groupListIds;
if (queryCondition && groupList) { groupListIds = getGroupListIds(groupList); }


var users = {};
var gr = new GlideRecord('sys_user_grmember');
if (groupListIds) { gr.addQuery('group', queryCondition, groupListIds); }
gr.query();
while (gr.next()) { users[gr.user.toString()] = true; }

var ids = [];
for (var id in users) { ids.push(id); }
return ids;
}


// get sys_ids for the named groups
function getGroupListIds(groupList) {
var ids = [];
var gr = new GlideRecord('sys_user_group');
gr.addQuery('name','IN',groupList);
gr.query();
while (gr.next()) { ids.push(gr.sys_id.toString()); }
return ids;
}

 

And then on the field I use as a reference, I added this reference qualifier:

find_real_file.png

 

Now, for some context. I am trying to display only users who are a member of the "Hiring Managers" group. But when I add this RQ, I still see all of the users when I do a lookup on my field. The group was created in my dev environment, outside of the custom app I am building, could that be the case? 

 

I tried changing the name of the group in the RQ to "hiring managers" or "hiring_managers" but have not had any luck.

 

Here's the group just for kicks. 

find_real_file.png

Since you are in a scoped application the use of reference qualifier functions in business rules isn’t permitted. Long ago an option for reference qualifier functions was to create a business rule on the “global” table and those functions would be cached upon login. There are several out of the box examples of this like getMyGroups and isApprovalMine. These are quite old and aren’t following best practices. The better solution, and only solution for scoped applications, is to create a helper/utility script include with those functions and then call that script include function in your reference qualifier. See this article for examples: https://docs.servicenow.com/bundle/london-platform-administration/page/script/server-scripting/concept/c_ReferenceQualifiers.html

This was extremely helpful and, quite honestly, ServiceNow should have included this with or instead of the version for Roles!

imranf1
Tera Contributor

Hello,

I deployed that bit I wrote as a global Business Rule, but the "When to run" tab isn't filled in. No filters, nothing is ticked - when I created the BR, I simply ignored the tab and filled in the code in the "Advanced" tab. For reference, this is exactly how the original BR I copied from looks like -- in your instance, find the BR named "getRoledUsers", it should have an update date of "30/10/2009 23:30:30" and updated by "pat.casey".

Your reference's qualifiers look alright - nearly identical to my own except for the different group name. My BR function name does have a prefix though, it's "my_getGroupedUsers" instead of just "getGroupedUsers", and the Reference qualifier javascript is also updated to match. I tend to add a prefix to all my stuff, helps when you're quickly skimming through code. Shouldn't matter though, I don't think there's an existing "getGroupedUsers" anyway.

Try emptying your "When to run" tab (remove any filters and uncheck whatever). Again, for reference, look at the existing "getRoledUsers" global Business Rule.

Steven Chaparro
ServiceNow Employee
ServiceNow Employee

Hello All,

I was trying to apply this solution too on my personal instance but when I created the script include under the GRC:Vendor Risk Management it didnt working. I decided to create the script include in the Global application, but by doing that, I got a different result, which was the error message:

find_real_file.png

I was able to fix my issue just by adding global on the qualifier field:

javascript:new global.GetGroupMember().getMember('group_sysID');

and with this configuration on the script include:

Name: GetGroupMember
API Name(read-only): global.GetGroupMember
Client callable: true
Application: Global
Active:true
Accessible: All application scopes (important if you want any application to use it like in my case was            GRC:Vendor Risk Management trying to access it)

If for some reason, you select "This application scope only" in the Accessible from field, you will get this errir instead:

find_real_file.png

I know this may sound a little like common sense, but Im hoping this is intended for those like the author and myself, that doesn't know a lot of scripting and we are still learning.


Thank you all for your posts that helped me get it working for my need.