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

Brent Sutton
Mega Sage

Hi Ya,

Give the following code a go in your if activity:

Code:

answer = ifScript();

function ifScript() {
	var grJob = new GlideRecord("u_cmdb_ci_batch_job");

	if (grJob.get(current.variables.job)) {
		var supportGrpSysId = grJob.support_group.toString(); //assumes the support group is reference field
		var userSysId = current.variables.requested_for.toString();
		
		/*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
		}
	}
	else {
		return "no"; //if the job can't be found then return "no"
	}
}

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. 

Hey Brent, thanks you so so much for your time and effort on this. I got this error when i tried to run it, i am going to see if i can figure out whats going on:

 

 

find_real_file.png

 

 

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.

Hey Brent,

Thanks again for your time, i really appreicate your help! I made one small tweak which was changing the line where the usersysid is being declared to:

var userSysId = current.request.requested_for.toString();

 

I have one more question if you have time, would it be possible for me to add a line of code to check if the requested for user is in another variable (authorized_users) in the same line of code or would i have to do a separate query?

find_real_file.png