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.

Show Incident records based on logged in user's group

tscoggin
Giga Contributor

On a Before Business Rule 

I'm trying to filter records that:

  1. Were created by logged in user
  2. The Project field value on a record = one of the logged in users groups
  3. If the logged in user belongs to group 1 also include records where project field where = to  'X"

The top 3 lines work but the "if statement" does not add to the query

var u = gs.getUserID(); //Get the sys_id value of the current user
var groups = gs.getUser().getMyGroups();
var gr = u.addQuery(current.addQuery('caller_id', u).addOrCondition('u_project_1', getMyGroups()));

if (gs.User().isMemberOf('Group 1')) {
u.addOrContition('u_project_1', 'Group 2');

1 ACCEPTED SOLUTION

This script running in a before Query Business Rule on the incident table will limit the results to only show those where:

  1. The Caller = the current user, OR
  2. The Project (custom reference field on sys_user_group) = one of the logged in user's groups, OR
  3. If the current user is a member of <group1>, then Project = <group2>
(function executeRule(current, previous /*null when async*/ ) {
	var gr = current.addQuery('caller_id', gs.getUserID()).addOrCondition('u_project_1', 'IN', getMyGroups());
    if(gs.getUser().isMemberOf('group1')){
		gr.addOrCondition('u_project_1', 'group2');
	}
})(current, previous);

View solution in original post

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

Try this - assuming 'Group 1' is replaced with the group sys_id.

if(gs.getUser().isMemberOf('Group 1')){

Still not pulling in the information from the "If statement".

I also tried using u.addQuery to see if that was it.

 

var u = gs.getUserID(); //Get the sys_id value of the current user
var groups = gs.getUser().getMyGroups();
var gr = u.addQuery(current.addQuery('caller_id', u).addOrCondition('u_project_1', getMyGroups()));

if(gs.getUser().isMemberOf('15f42a071XXXXX10b0fc6579bc4bcbfc')){
u.addQuery('u_project_1', '937100bc1XXXXX0b0fc6579bc4bcb31');
}

Shouldn't the following:

var gr = u.addQuery(current.addQuery('caller_id', u).addOrCondition('u_project_1', getMyGroups()));

be

var gr = current.addQuery('caller_id', u).addOrCondition('u_project_1', "IN" , groups.join() );

When I try that I only get two records. Down from 76. I should have about 80.