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

Abhijit4
Mega Sage

Try using group sys_id instead of group name.

Let me know if you have any further questions.

Please mark this as Correct or Helpful based on the impact.

Regards,
Abhijit
Community Rising Star 2022

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hello Abhijit,

I tried it with sys_id instead of name but the result is the same.

@Bart Jan Bultman IS THIS A SCOPED APP ?

if its as scoped app is member of wont work you need to follow below script

IF YES THEY YOU CAN TRY THIS SCRIPT 

var gr =  new GlideRecord('sys_user_grmember');
gr.addQuery('user',gs.getUserID()); 
gr.addQuery('group', 'gorup_sys_id');
gr.query();

if(gr.next()) {
    answer = true;
}
else {
   answer = false;
}

hope this helps you

 

Hello @Mohith Devatte. Result is the same, it sends no notification with this code.

I'm only using a scheduled job, global event and notification. I did not do anything with scope. Should I change that and how/where do I do that?

Vasantharajan N
Giga Sage
Giga Sage

Which table you have written a notification. If that table contains a field that referenced to the sys_user table then you can use the below code in advanced script

var user_sys_id = current.field_name;
var userOb = new GlideUser().getUserByID(user_sys_id);

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

Thanks & Regards,
Vasanth