Flow to send notification to managers with a list of staff

Biddaum
Tera Guru

I have a requirement to send an email on an adhoc basis to managers of staff that have a staff member who's end date is coming up (we have a large contractor staff base).

 

This is to be instigated by another team and we store the end dates in the ServiceNow profile in a custom field called "u_accountexpirydate" or display name of "Account Expiry".

 

I have a notification set to be triggered by a flow called "IT account expiry notification" 

 

The flow is being triggered by a catalog item that just has the date that is to be used to find all active accounts with an expiry date at or before that date.

 

I have used a look up records action on the sys_user table and can find the records and can generate an individual email to a manager for each staff member they manager, however that for example might mean that one manager will get 30 separate emails for 30 separate staff who have end dates.

 

Anyone able to give me an idea of how I could make it so it would loop through, find all staff for a single manager, send a notification to that manager with their staff listed, then move onto the next manager and so forth untill all managers are notified of their staff if they are to expire?

 

 

1 ACCEPTED SOLUTION

pr8172510
Tera Guru

Hi @Biddaum,

 Right now your flow is sending one notification per expiring user because the notification action is inside the user loop.

Current behavior:

Look Up Records → For Each User → Send Notification

So if one manager has 30 expiring staff members, they receive 30 separate emails.

A better approach is to:

  • Get all expiring users
  • Group them by manager
  • Send one consolidated email per manager containing all their staff

 flow structure:

Trigger Catalog Item
    ↓
Get Catalog Variables
    ↓
Look Up Records (expiring users)
    ↓
Group users by manager
    ↓
Loop through managers
    ↓
Send ONE notification per manager

You can handle the grouping using a Script step or reusable Flow Action.

Example grouping logic:

var managerMap = {};

while (gr.next()) {

    if (!gr.manager)
        continue;

    var mgr = gr.manager.toString();

    if (!managerMap[mgr]) {
        managerMap[mgr] = [];
    }

    managerMap[mgr].push({
        name: gr.name.toString(),
        expiry: gr.u_accountexpirydate.toString()
    });
}

Then loop through each manager and build a single email with all staff listed.

 

You could also implement this as:

  • Flow Designer + Script Action (recommended)
    OR
  • Scheduled Script Execution if this becomes a regular batch process.

 

View solution in original post

4 REPLIES 4

pr8172510
Tera Guru

Hi @Biddaum,

 Right now your flow is sending one notification per expiring user because the notification action is inside the user loop.

Current behavior:

Look Up Records → For Each User → Send Notification

So if one manager has 30 expiring staff members, they receive 30 separate emails.

A better approach is to:

  • Get all expiring users
  • Group them by manager
  • Send one consolidated email per manager containing all their staff

 flow structure:

Trigger Catalog Item
    ↓
Get Catalog Variables
    ↓
Look Up Records (expiring users)
    ↓
Group users by manager
    ↓
Loop through managers
    ↓
Send ONE notification per manager

You can handle the grouping using a Script step or reusable Flow Action.

Example grouping logic:

var managerMap = {};

while (gr.next()) {

    if (!gr.manager)
        continue;

    var mgr = gr.manager.toString();

    if (!managerMap[mgr]) {
        managerMap[mgr] = [];
    }

    managerMap[mgr].push({
        name: gr.name.toString(),
        expiry: gr.u_accountexpirydate.toString()
    });
}

Then loop through each manager and build a single email with all staff listed.

 

You could also implement this as:

  • Flow Designer + Script Action (recommended)
    OR
  • Scheduled Script Execution if this becomes a regular batch process.

 

Ankur Bawiskar
Tera Patron

@Biddaum 

call flow action and use script to send 1 email per manager

check below links where I shared solution for something similar, and enhance

Email script 

Email Notification to all users of assets assigned specifically to them 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Biddaum 

Thank you for marking my response as helpful.

I believe I also shared a working solution and approach

Did you get a chance to check above links?

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

Biddaum
Tera Guru

ultimately I created a catalog item, that just has a date field.

Then a flow that is triggered by the catalog item, the date is ingested via a get catalog variables action.

I created a custom action with a script that checks the expiry date, gets their manager, groups users by manager.

It then creates an event per manager that triggers a notification and attaches a copy of each notification to the RITM, then closes off the RITM.