How to Exclude Users from Notification, Based on Group Membership in a Child Table (ServiceNow)

ronro2
Tera Contributor

Hi!

I have a notification in servicenow based on child table x_vgll_decom_decommission_task (child of x_vgll_decom_decommission).

----------------------------------------------------------------------------------------------------------

So every time a task is created, a notification is to assigned_to on the task, with the exception of when:

 

assigned_to  is included in assignment_group 'Lifecycle Management Technical Management' (Sys ID eeaac199e0dc1614068b7138fc9661dd)

 

OR

 

If assigned_to is empty, but assignment_group is eeaac199e0dc1614068b7138fc9661dd.


AND 

 

If 'decommission.responsible_tech_user_selected' (on parent) IS True.
------------------------------------------------------------------------------------

Keep in mind that if a user (assigned_to) does not belong to "Lifecycle Management Technical Management", they should receive the email notification, even though 'responsible_tech_user_selected' on the parent is true/checked.

Also, keep in mind that the reference field to the parent on the child is called 'decommission' and that the recipients are: assignment_group and assigned_to.

My event that should be triggered by the business rule is called 'x_vgll_decom.resp_tech_user_selected'. My BR script is not working though, what is wrong?  

---------------------------------------------------------------------------------------
Business rule configuration: 
- Table: x_vgll_decom_decommission_task

- When to run: After Insert
- Conditions: left out

Here is my Business Rule script: 

(function executeRule(current, previous /*null when async*/) {

    var assignedTo = current.assigned_to;
    var assignmentGroup = current.assignment_group;
    var parent = current.decommission.getRefRecord();

    // Check if responsible_tech_user_selected is true on parent
    if (!parent || !parent.responsible_tech_user_selected) {
        return;
    }

    // If assigned_to is in Lifecycle Management Technical Management group, skip
    var lifecycleGroupId = 'eeaac199e0dc1614068b7138fc9661dd';

    var skipNotification = false;

    if (assignedTo) {
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', assignedTo);
        gr.addQuery('group', lifecycleGroupId);
        gr.query();
        if (gr.next()) {
            skipNotification = true;
        }
    } else if (assignmentGroup == lifecycleGroupId) {
        skipNotification = true;
    }

    if (!skipNotification) {
        gs.eventQueue('x_vgll_decom.resp_tech_user_selected', current, current.assigned_to, current.assignment_group);
    }

})(current, previous);


Notification:
- Send when: Event is fired

- Event name: x_vgll_decom.resp_tech_user_selected
- Who will receive; Users/Groups in fields: assigned_to, assignment_group

- Event parm 1: True

- Event parm 2: True
----------------------------------------------------------------------------------

But the notification does not trigger at all when tasks are created, no matter what type of user is in the assigned_to. So what is wrong? I bet there is something wrong with the BR Script. But I can't understand what it is. 

I am thankful for any kind of input! 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ronro2 

try this and share the logs

(function executeRule(current, previous /*null when async*/) {

    var assignedTo = current.assigned_to.toString();
    var assignmentGroup = current.assignment_group.toString();
    var parent = current.decommission.getRefRecord();

    // Check if responsible_tech_user_selected is true on parent
    if (!parent || !parent.responsible_tech_user_selected) {
        gs.info("[BR] Skipping: Parent missing or responsible_tech_user_selected is not true.");
        return;
    }

    var lifecycleGroupId = 'eeaac199e0dc1614068b7138fc9661dd';
    var skipNotification = false;

    if (assignedTo) {
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', assignedTo);
        gr.addQuery('group', lifecycleGroupId);
        gr.query();
        if (gr.hasNext()) {
            skipNotification = true;
            gs.info("[BR] Skipping: assigned_to is in Lifecycle Management Technical Management group.");
        }
    } else if (assignmentGroup == lifecycleGroupId) {
        skipNotification = true;
        gs.info("[BR] Skipping: assignment_group is Lifecycle Management Technical Management group and assigned_to is empty.");
    }

    if (!skipNotification) {
        gs.info("[BR] Firing event: x_vgll_decom.resp_tech_user_selected for task " + current.number);
        gs.eventQueue('x_vgll_decom.resp_tech_user_selected', current, assignedTo, assignmentGroup);
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@ronro2 

try this and share the logs

(function executeRule(current, previous /*null when async*/) {

    var assignedTo = current.assigned_to.toString();
    var assignmentGroup = current.assignment_group.toString();
    var parent = current.decommission.getRefRecord();

    // Check if responsible_tech_user_selected is true on parent
    if (!parent || !parent.responsible_tech_user_selected) {
        gs.info("[BR] Skipping: Parent missing or responsible_tech_user_selected is not true.");
        return;
    }

    var lifecycleGroupId = 'eeaac199e0dc1614068b7138fc9661dd';
    var skipNotification = false;

    if (assignedTo) {
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', assignedTo);
        gr.addQuery('group', lifecycleGroupId);
        gr.query();
        if (gr.hasNext()) {
            skipNotification = true;
            gs.info("[BR] Skipping: assigned_to is in Lifecycle Management Technical Management group.");
        }
    } else if (assignmentGroup == lifecycleGroupId) {
        skipNotification = true;
        gs.info("[BR] Skipping: assignment_group is Lifecycle Management Technical Management group and assigned_to is empty.");
    }

    if (!skipNotification) {
        gs.info("[BR] Firing event: x_vgll_decom.resp_tech_user_selected for task " + current.number);
        gs.eventQueue('x_vgll_decom.resp_tech_user_selected', current, assignedTo, assignmentGroup);
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar 

[BR] Skipping: assigned_to is in Lifecycle Management Technical Management group.
[BR] Skipping: assigned_to is in Lifecycle Management Technical Management group.
[BR] Skipping: assigned_to is in Lifecycle Management Technical Management group.
[BR] Firing event: x_vgll_decom.resp_tech_user_selected for task DECTSK0002767  <-- here is a user in the assigned_to that does not belong in that group, so the notification should have been fired. 

And these are the logs for each assigned_to when 'responsible_tech_user_selected' is not true:

[BR] Skipping: Parent missing or responsible_tech_user_selected is not true.
[BR] Skipping: Parent missing or responsible_tech_user_selected is not true.

[BR] Skipping: Parent missing or responsible_tech_user_selected is not true.

[BR] Skipping: Parent missing or responsible_tech_user_selected is not true.

@ronro2 

did you check what value comes for that flag?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar hey, the value it holds is the sys_id of an assigned_to person who does not belong to the exception assignment group "Lifecycle Management Technical Management". Which is correct. But he still does not get the email notification. 

This is screenshot of event log:

ronro2_0-1748521888381.png