Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Article [2] -> Flow Designer: Weekly Incident Summary Email per Assignment Group

Mohammed8
Mega Sage

Hi all, 

Inspired by @Aniket Chavan  Article  (practical use case by him), I decided to approach in similar way for Assignment group using Flow Designer. In this article, I walk through the complete step-by-step implementation.

 

Use-Case: Every Monday morning, each assignment group should receive one consolidated email containing all incidents assigned to their group. Additionally, all users belonging to that group must be added to CC. Built purely using just Flow Designer steps

 

Step Configure the trigger: Start by defining when the flow should run. 

  • Trigger Type: Weekly

  • Day Of Week: Monday

  • Time: 05:00:00 (any preferred time)

This ensures the flow executes automatically every Monday morning and sends the latest incident summary to each assignment group.

 

Mohammed8_0-1763955627735.png

 

Action Step 1 : Lookup Records on the sys_user_group table with a scripted filter to find only groups who have assigned incidents. I used Flow Designer’s Advanced Condition script option

 

Mohammed8_1-1763955819946.png

 

Here is the script for advanced condition for above step:

 

/*

** Return all unique assignment groups that currently have incidents

** Output must be an encoded query for sys_user_group

*/

var agg = new GlideAggregate('incident');

agg.addAggregate('COUNT');                 // Needed for GA to run

agg.addNotNullQuery('assignment_group');   // Only incidents with a group

agg.groupBy('assignment_group');           // Group by assignment group

agg.query();

var groupIds = [];

while (agg.next()) {

    var gid = agg.getValue('assignment_group');

    if (gid) {

        groupIds.push(gid);

    }

}

// Build encoded query for sys_user_group

if (groupIds.length > 0) {

    return 'sys_idIN' + groupIds.join(',');

} else {

    return 'sys_idINnone'; // Safely return no results

}

 

Action Step 2: Iterates through the lookup results from Action Step 1. 

Action Step 3 : After iteration using lookup record on sys_user_grmember to find users of each group

 

Mohammed8_2-1763955933039.png

 

Action 4: Iterates through the lookup results from Action Step 3. 

Action 5: Set flow variable with mail of each user of group

 

Mohammed8_3-1763956279509.png

 

Here is script for flow variable:

 

var emails = fd_data.flow_var.variable || [];

emails.push(fd_data._4__for_each.item.user.email);

return emails;

 

Action Step 6: Select the Send Email action.
Set To = group email (dot-walk from Action Step 2).
Set CC = the flow variable from Action Step 5 (convert it to a string as shown below)

 

Mohammed8_4-1763956631101.png

 

Continue: Action Step 6 : For Body of email toggle write script as below

 

Mohammed8_5-1763956906904.png

 

you can tweak this as per your use case

 

 var groupName = fd_data._2__for_each.item.name;  // data pill: Group name  

    var groupSysId = fd_data._2__for_each.item.sys_id; // data pill: Group sys_id

    var incGr = new GlideRecord('incident');

    incGr.addQuery('assignment_group', groupSysId);

    // incGr.addQuery('state', '!=', 7); // Skip closed (optional)

    incGr.query();

    var count = 0;

    var message = "<p>Hi " + groupName + " Team,</p>";

    message += "<p>This is your <strong>weekly incident summary report</strong> from ServiceNow.</p>";

    if (!incGr.hasNext()) {

        message += "<p><em>Your group does not have any open incidents at this time.</em></p>";

    } else {

        message += "<table border='1' cellpadding='4' cellspacing='0' style='border-collapse:collapse; width:100%;'>";

        message += "<tr>" +

                   "<th>#</th>" +

                   "<th>Incident Number</th>" +

                   "<th>Short Description</th>" +

                   "<th>State</th>" +

                   "<th>Created On</th>" +

                   "</tr>";

        while (incGr.next()) {

            count++;

            // Truncate long description

            var desc = incGr.getDisplayValue('short_description') || "";

            if (desc.length > 70) {

                desc = desc.substring(0, 70) + "...";

            }

            message += "<tr>";

            message += "<td>" + count + "</td>";

            message += "<td>" + incGr.getDisplayValue('number') + "</td>";

            message += "<td>" + desc + "</td>";

            message += "<td>" + incGr.getDisplayValue('state') + "</td>";

            message += "<td>" + incGr.getDisplayValue('sys_created_on') + "</td>";

            message += "</tr>";

        }

        message += "</table>";

    }

    message += "<br><p>Regards,<br><strong>ServiceNow Notification System</strong></p>";

    return message;

 

Action step 7: Clear the variable for next iteration

 

 

Mohammed8_11-1763958450416.png

 

Here is the full flow view:

 

Mohammed8_12-1763958702983.png

 

Result: All steps completed successfully

 

Mohammed8_6-1763957072551.png

 

Email sent to Group email and users are copied as shown below

 

Mohammed8_7-1763957549147.png

 

Email Preview:

 

Mohammed8_9-1763957851871.png

 

Cross check on incident table list, it matches

 

Mohammed8_10-1763957891860.png

 

Note:

The group email must not be empty. If the group has no email configured, the steps above will fail and the flow will throw an error

 

I appreciate your support, and I’m open to comments or suggestions to continue improving and learning

 

Thanks and Regards,

Mohammed Zakir

 

14 REPLIES 14

Gary Larsen
Mega Sage

Hello Mohammed8

I am getting an error I just saw your note about group email must not be empty. How could I achieve this when we don't use group email field normally we just email individuals in the group  

 

@Gary Larsen 

If you don’t have a group email, one simple suggestion is to follow the Action step-6 approach I used above for CC field but apply that same step to the TO field instead.

 

Regards,

Mohammed Zakir

not sure what I am doing wrong including shots of my config and the error

@Gary Larsen 

1) Do a test with manual entering email

For the first check put a manual email address like (xyz@gmail.com) in the TO field and run test and check if error

 

2) Check if  group has users

This issue may occur if the any of group does not have users. 

 

3) I hope your flow variable type is string. Do cross check for each step in test whether u r getting proper results or not

 

Do let me know

Regards,

Mohammed Zakir