Fire Event should be trigger once in Flow Designer

rmaroti
Tera Contributor

Hi Everyone,

 

I have a requirement to implement an action for each active group record. After processing each group, I need to check if the group has an active manager. If an active manager exists, a notification should be triggered via the "Fire Event" in FD.

 

However, I want to avoid triggering the notification multiple times when a manager has multiple  assigned groups. Since the system iterates over each group and the notification is triggered each time a group with an active manager is found, the notification may be fired multiple times for the same manager because it has assign multiple group.

 

The goal is to restrict the "Fire Event" to trigger only once per manager, even if they are assigned to multiple groups.

 

I have implemented the FD as shown in the screenshot below. Could you please review it and suggest any changes or improvements to achieve this functionality?

1 ACCEPTED SOLUTION

@rmaroti 

Hope you are doing good.

Did my reply answer your question?

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

4 REPLIES 4

Shivalika
Mega Sage

Hello @rmaroti 

 

You can use below 👇 script to get aall active groups unique managers in an array. And then run for each to send mail once to all. Since it's unique manager array that its returning no double mail. 

 

You can modify the condition as per your requirement - 

 

var uniqueManagers = {};
var mgrNames = {};

var gr = new GlideRecord('sys_user_group');
gr.addQuery('active', true);
gr.query();

while (gr.next()) {
var manager = gr.getValue('manager');
if (manager && !uniqueManagers[manager]) {
uniqueManagers[manager] = true;

var user = new GlideRecord('sys_user');
if (user.get(manager)) {
mgrNames[manager] = user.getDisplayValue();
}
}
}

for (var id in mgrNames) {
gs.info('Manager: ' + mgrNames[id] + ' | sys_id: ' + id);
}

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Ankur Bawiskar
Tera Patron
Tera Patron

@rmaroti 

not possible using flow designer.

Please use daily scheduled job or frequency as per your customer requirement

something like this but please enhance

I hope you know how to create event, notification linked to that event and you can enhance it further

(function executeJob() {
    // Create a map to group active groups by their manager
    var managerGroupMap = {};

    // Query all active groups
    var groupGr = new GlideRecord('sys_user_group');
    groupGr.addQuery('active', true); // Only active groups
    groupGr.query();
    while (groupGr.next()) {
        var managerSysId = groupGr.manager.toString(); // Get the manager's sys_id
        var groupName = groupGr.getDisplayValue('name'); // Get the group's name

        if (!managerSysId) {
            continue; // Skip groups without managers
        }

        // Add group names to the manager's list in the map
        if (!managerGroupMap[managerSysId]) {
            managerGroupMap[managerSysId] = [];
        }
        managerGroupMap[managerSysId].push(groupName);
    }

    // Iterate through the map and send one email per manager
    for (var managerSysId in managerGroupMap) {
        var groupList = managerGroupMap[managerSysId].join("\n"); // Create a list of groups

        // Get the manager's record
        var managerGr = new GlideRecord('sys_user');
        if (managerGr.get(managerSysId)) {
            var emailAddress = managerGr.email;

            if (emailAddress) {
                // Fire an event to send the email notification
                gs.eventQueue('send_manager_email', null, emailAddress, groupList);
                gs.info("Email triggered for Manager: " + emailAddress + " with Groups:\n" + groupList);
            } else {
                gs.warn("Manager does not have an email address: " + managerSysId);
            }
        }
    }
})();

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

@rmaroti 

Hope you are doing good.

Did my reply answer your question?

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

@rmaroti 

Hope you are doing good.

Did my reply answer your question?

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