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.

Business rule to view records of those groups of which loggedin user is not member of

rishabh31
Mega Sage

Hello Community members,

I want to create a BR (client callable) for a custom dynamic filter option 'Not one of my Group', which displays only those records of which loggedin user is NOT a member of.

For this I tried below useful article (refer URL- 

@https://www.servicenow.com/community/in-other-news/2-steps-to-create-a-dynamic-filter-that-returns-groups-lo...

 

but not getting the desired results.

Requesting to please help with the BR scripting part to satisfy the above explained filter condition.

 

Will mark the desired response as helpful & accepted!

 

Thanks in Advance.

 

 

1 ACCEPTED SOLUTION

Okay, so you want it to be available as a dynamic filter,

Let's make some minor changes, to make it work, see example below.

 

 

Script include:

var MembershipUtil = Class.create();
MembershipUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	notMyGroups : function (){
		var notMemberOf = [];

		var groupGR = new GlideRecord('sys_user_group');
		groupGR.query();

		while (groupGR.next()){
			var memberGR = new GlideRecord('sys_user_grmember');
			memberGR.addEncodedQuery('user=' + gs.getUserID() + '^group=' + groupGR.getUniqueValue());
			memberGR.query();
			if (!memberGR.hasNext()){
				notMemberOf.push(groupGR.getUniqueValue());
			}
		}

		return notMemberOf;  // returns sysID of all groups which logged in user is not member of 	
	},
	
    type: 'MembershipUtil'
});

 

Dynamic filter:

dynamic-filter-not-my-groups.png

View solution in original post

7 REPLIES 7

Thank you for your kind words @rishabh31 !

The changes I did was to reflect it to be available as a dynamic filter.

In my first response I thought you needed a script include that returned all sysID of groups that a specific (function input) user was not member of.

The result was returned as a comma-separated string.

In this updated script, I instead went without parameters, and went with the currently logged in user (gs.getUser...)
And the results are returned as an Array of the sysID of the matching groups.

Bert_c1
Kilo Patron

If you want a comma-separated list of group names, as the link you have shows, BR script logic follows:

 

// Get list of groups the logged in user is not a member of
//
var uID = gs.userID();
gs.info("script: User = " + uID);

// get the user groups
var sgm = new GlideRecord("sys_user_grmember");
var userGroups = [];
sgm.addQuery('user', uID);
sgm.query();
while (sgm.next()) {
   userGroups.push(sgm.group.getDisplayValue());
}
gs.info("script: userGroups = " + userGroups + ".");

// Now query group table for the remaining groups
var grpr = new GlideRecord('sys_user_group');
// Build query to exclude the groups
var tQuery = '';
for (i=0; i<userGroups.length; i++) {
   tQuery += "name!=" + userGroups[i] + "^";
}
grpr.addEncodedQuery(tQuery);
grpr.query();
gs.info("script: processing " + grpr.getRowCount() + " user groups.");

// Get list of groups to user in filter
var queryGrps = '';
while (grpr.next()) {
   queryGrps += grpr.name + ",";
   gs.info("group " + grpr.name + ".");
}
gs.info("queryGrps = " + queryGrps + "." );

(comment out or remove the gs.info() debug messages).

Bert_c1
Kilo Patron

OLaN's solution may be better that a BR, for a Dynamic Filter.