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.

Notification advanced condition script, only send when user is part of a

Bart Jan Bultma
Kilo Sage

Im kicking off a event from a scheduled job that checks for a users end date daily. If the end date is today, it should send a notification to our ServiceDesk. This part works.

In the Notification I am adding an advanced condition that should only send the notification if the user that is on its end date is a member of a specific group in ServiceNow. I am using the code below but it always returns false and does not send the notification.

 

var groupMember = gs.getUser();

if(groupMember.isMemberOf('GROUPNAME'))
{
    answer = true;
} 
else 
{
    answer = false;
}

.

 

 

1 ACCEPTED SOLUTION

@Bart Jan Bultman - Could you please try with the below code if the notification is on table sys_user. 

Note:

1. Assuming you are trying to trigger notification for the user record. 

2. Change the variable group_name value and use the code.

var group_name = "Please set your group name here to check";
var user_sys_id = current.sys_id;
var userOb = new GlideUser().getUserByID(user_sys_id);

if (userOb.isMemberOf(group_name)) {
    answer = true;
} else {
    answer = false;
}

Thanks & Regards,
Vasanth

View solution in original post

12 REPLIES 12

The notification is on table sys_user. I tried this code aswell but it doesnt seem to work for me. I think I have something set up wrong that is making the code always return false or always return true, regardless of the groups that users are in.

@Bart Jan Bultman - Could you please try with the below code if the notification is on table sys_user. 

Note:

1. Assuming you are trying to trigger notification for the user record. 

2. Change the variable group_name value and use the code.

var group_name = "Please set your group name here to check";
var user_sys_id = current.sys_id;
var userOb = new GlideUser().getUserByID(user_sys_id);

if (userOb.isMemberOf(group_name)) {
    answer = true;
} else {
    answer = false;
}

Thanks & Regards,
Vasanth

I am not quite sure why, but making the group name into a variable and then using it solved it! This works exactly as wanted!

Thank you for your help.