Notification to Trigger When a User Gains a Direct Report ONLY If They Previously Had Zero

Josh Johnson
Giga Expert

I need to craft a notification to tell me if someone becomes a manager who previously wasn't one. Direct Reports are synced in from Okta, so this change can happen at any time. There are specific licenses and conversations that need to be addressed when a user gains direct reports.  If a user already has existing direct reports, we don't care because they've already gone through this process. We need to trigger when that value changes from 0. I'm sure this will need to be done through scripting but haven't had much luck building anything functional yet. Additional complexity because SN doesn't have a field for Direct Reports that I can find, just Managers. So it seems like this has to be built from the trigger of a change or creation of a users manager, then checking if that manager had any other direct reports at that time. Any help is appreciated.

 

This is what I've tried but it doesn't seem to fire:

 

// Check if the manager field has been changed to a non-empty value
if (current.manager.changes() && current.manager) {
    // Check if the previous value of the manager field was empty (user was not a manager before)
    if (!previous.manager) {
        // Return true to trigger the notification
        true;
    }
}

// If the condition is not met, return false to prevent the notification from being sent
false;
2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, I would add a custom field on sys_user table to 'count' the number of reports a user has (as I expect that someone will want to know this in the future) and use an after insert\update BR on sys_user to set this when a manager changes utilizing glideAggregate for the count. You can then trigger a sysevent for your notification from the same BR, or add another BR on sys_user to trigger it when the count changes from 0.

Untested, so may have syntax errors but something like this.

var managerList = [];

if (current.manager != '') {
    managerList[0] = current.manager.sys_id;
}
if (previous.manager != '') {
    managerList[1] = previous.manager.sys_id;
}

for (i = 0; i < 2; i++) {
    //No reason to do anything if the current\previous manager field is blank
    if (managerList[i] != '') {
        //count the managers reports
        var managerCount = 0;
        var reportCount = new GlideAggregate('sys_user');
        reportCount.addAggregate('count', 'manager');
        reportCount.addQuery('manager', managerList[i]);
        reportCount.query();

        managerCount = reportCount.getTotal('COUNT');
        gs.info(managerCount);

        var managerUpdate = new GlideRecord('sys_user');
        //make sure we find a matching user record (should always be true but best practice to check)
        if (managerUpdate.get(managerList[i])) {
           //make a note of existing report count incase we need to trigger a notification
            var previousReports = managerUpdate.u_custom_field;
            managerUpdate.u_custom_field = managerCount;
            managerUpdate.update();
           //trigger a notification if needed.
            if (previousReports == 0) {
                // trigger a sysEvent for notifications
		//https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_GlideSystemAPI#r_GS-eventQueue_S_O_S_S_S?
            }
        }
    }
}

 

I created a new Integer field called u_number_of_direct_reports that counts the direct reports a user in sys_user has. This is being achieved by a Calculated Value:

 

javascript&colon; current.sys_user_direct_reports = new GlideRecord('sys_user');
current.sys_user_direct_reports.addQuery('manager', current.sys_id);
current.sys_user_direct_reports.query();
var directReportsCount = current.sys_user_direct_reports.getRowCount();
directReportsCount;

 

This is working well and the count is updating in real time as I add or remove direct reports. I created a notification and gave it the "When to Send" condition in Condition Builder of "Number of Direct Reports/ Changes From/ 0". The idea being that the only time I want this notification sent is if a user who doesn't have any direct reports (0), becomes a manager (any number that isn't 0).

 

Even though I can see the change to this field in the Event Log, the notification doesn't fire. Is there an issue with using integers in the "Changes" style conditions? Something else I may be overlooking? To ensure everything else in the notification was ok, I set Condition Builder to "Manager/ Changes" and it fired correctly then.