Using Advanced Notification Script to Limit Based on User's Role

JayGervais
Kilo Sage

Hi there, 

There are a number of notifications that I am trying to limit based on the role of the user it is being sent to. New employees in the process of being onboarded do not have access to our Service Portal so we are trying to limit certain notifications from sending to these users, which provide links they can not access. The limitation is based on if they have a certain role that is given to new hires: sn_hr_sp.hrsp_new_hire. 

If the user has this role, I want to prevent the notifications from sending to them. I have tried multiple variations in the advanced condition section of the notifications but these still seem to send to the user anyway. Am I doing something wrong, or is it even possible for me to do what I am trying to?

Here are a couple of the variations I have tried:

1. This checks the role of the opened_for user to see if they have the new hire role. I have tried using answer both by declaring it as a variable and not. 

var openedFor = current.opened_for.user;
var answer = false;

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('role', 'a37b3f710b03120025666f3ef6673abf'); // sn_hr_sp.hrsp_new_hire
gr.addQuery('user', openedFor);
gr.query();
if (gr.next()) {
  answer = false;
} else {
  answer = true;
}

2. This is attempting to check the user's role and return true if it does not exist. This is more of a generic attempt I believed could be added to the notification. I have tried various formats of this with this being my most recent.

answer = false;
if (gs.hasRole('sn_hr_sp.hrsp_new_hire') == false) {
  answer = true;
}

Please let me know if I am doing something wrong.

Thank you,

Jay

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

Just to clarify, this condition would determine if the notification would be sent at all. Not just to a specific user, but to anyone at all.

With that said, for your first version, is "opened_for" the correct name of the field? If so, is that a reference field, if so, is there a need to dot-walk even further to "user"?

Would: current.opened_for -- not suffice? As that would return a sys_id, and on the sys_user_has_role, that too is a reference, thus a sys_id and that would be what it's looking for.

For your 2nd version, that would only work if the person who opened it has the role, not who it's "opened_for". So if a manager or someone opened it, the notification would still go out.

Please review the first version and consider tweaking.

Please also use log entries if you're still having issues and check your values to see what you're getting and then go and search the list view and see if you're finding the user, etc.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hi,

Just to clarify, this condition would determine if the notification would be sent at all. Not just to a specific user, but to anyone at all.

With that said, for your first version, is "opened_for" the correct name of the field? If so, is that a reference field, if so, is there a need to dot-walk even further to "user"?

Would: current.opened_for -- not suffice? As that would return a sys_id, and on the sys_user_has_role, that too is a reference, thus a sys_id and that would be what it's looking for.

For your 2nd version, that would only work if the person who opened it has the role, not who it's "opened_for". So if a manager or someone opened it, the notification would still go out.

Please review the first version and consider tweaking.

Please also use log entries if you're still having issues and check your values to see what you're getting and then go and search the list view and see if you're finding the user, etc.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen, 

Thank you for the reply and for clarifying these things for me.

I thought it would be possible to use the advanced query to limit emails from sending to particular users, but this does not seem to be the case. Do you know of any way this might be possible?

For current.opened_for.user, I did this because the field on the HR Case is referencing the HR Profile table and not the User table, so the dot-walk was used to get the user sys_id from the User table.

This insight has been very helpful. I appreciate it!