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.

Check if user in requested for field is apart of a group based off another field?

othomas1
Kilo Guru

Hello everyone, i am trying to come up with a workflow query that will allow me to:

- To check if the requester is in the Support Group or this new "Authorized Users" slush bucket of the Batch Job.

- If not, then an approval would need to go to the Managed By for the Batch Job

-The requested for field is a reference field on the catalog item, "support_group" and "batch job" are on the "u_cmdb_ci_batch_job" table. 

I basically want to look at the requested for field, the batch job field, and support_group field to see if the users belongs to the group based off of the selection in the batch job field.

find_real_file.png

This is the code i tried, but it didn't yield any results at all:

answer = ifScript();


function ifScript() {

 

var gr = new GlideRecord('u_cmdb_ci_batch_job');
gr.addQuery('name', current.variables.job);
gr.addQuery('support_group', current.variables.requested_for);
gr.query();

{
return 'yes';
}
return 'no';

}

Can anyone see if i am heading in the right direction? Any feedback would be greatly appreciated.

1 ACCEPTED SOLUTION

Brent Sutton
Mega Sage

Hi Ya,

I've had another go at simplifying the if activity script using an IIFE and dot walking to the support group information:

answer = (function(current) {

	var supportGrpSysId = current.variables.job.support_group.toString(); //assumes the support group is reference field. Dot walk to the support group through the job reference field
	var userSysId = current.variables.requested_for.toString(); //assumes requested_for is reference field.

	/*Determine if the requested_for user is part of the support group assigned to job*/
	var grGrpMem = new GlideRecord("sys_user_grmember");
	grGrpMem.addQuery("group",supportGrpSysId);
	grGrpMem.addQuery("user",userSysId);
	grGrpMem.setLimit(1); //only need to return one record from the query
	grGrpMem.query();

	if (grGrpMem.next()) {
		return "yes"; //return "yes" if they are part of the group
	}
	else {
		return "no"; //return "no" if they are not part of the group
	}
})(current);

Give that a go and let me know how you get along,

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

View solution in original post

5 REPLIES 5

Hi Ya,

I assume that authorized_users is a variable on the catalog item? If so, I would check the requested_for sys_id against this variable first as we don't need to go any further if we get a match. The following should work:

Code:

answer = (function(current) {
	var userSysId = current.request.requested_for.toString(); // sys_id of the requested_for user.
	var authorizedUsers = current.variables.authorized_users.toString(); //comma separated list of authorized users sys_id's.

	/*Do the authorized users check first. If the user is listed in this variable then we don't need to go any further
	* The index will be greater than 0 if we get a match on the users sys_id
	*/
	if (authorizedUsers.indexOf(userSysId) > 0) {
		return "yes";
	}

	var supportGrpSysId = current.variables.job.support_group.toString(); //assumes the support group is reference field. Dot walk to the support group through the job reference field

	/*Determine if the requested_for user is part of the support group assigned to job
	*/
	var grGrpMem = new GlideRecord("sys_user_grmember");
	grGrpMem.addQuery("group",supportGrpSysId);
	grGrpMem.addQuery("user",userSysId);
	grGrpMem.setLimit(1); //only need to return one record from the query
	grGrpMem.query();

	if (grGrpMem.next()) {
		/* return "yes" if they are part of the job support group or are one of the authorized users*/
		return "yes"; 
	}
	else {
		return "no"; //return the result
	}

})(current);

Let me know how you get along.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.