Send One Email per Person in a Mixed List

apply to eachOne of the most common questions I see on the Power Platform forums is: “I have a data source that contains a list of information that I need to send as an HTML table to the individual mentioned in each item. But its sending more than one email to each person.  How do I get it to send just one email for each person with all the records for that person?”  In this post I’ll demonstrate a general method to accomplish that goal using Power Automate. For this example we’ll use a simple list from SharePoint, but the same technique should work for any data source that can be sorted.  Here’s our list:

list

We want to send a table with the hours spent by each programmer and their current status to each Project Manager.

Step#1 – Retrieve the records from data source.  Sort them based on the field that references the person, in this case Project Manager. This will not work if the field includes multiple people. Here’s a screenshot of that part of the flow.

list1

Step#2 – The next step is to get a list of the distinct project managers in the results we have retrieved.  Unfortunately, Power Automate doesn’t have a Distinct() function.  But we can achieve the same result using a combination of a Data Select action and the Union() function. When we combine the Data Select array with itself using Union() we will get an array that has a single instance of each unique record in the Data Select array. In this case a list of each Project Manager’s Email address.

Distinct1

Step#3 – Now that we have an array of distinct Project Manager Emails we can run an Apply to Each loop on each Email, extract the records for that manager, create a table and send the email. Here’s what the overall Loop looks like then we’ll review each step. The loop runs on the array produced by the union in the last Compose action.

loop 1

Loop Step #1 – The first thing we do inside the loop is filter the original set of records to create an array of records where the Project Manager’s Email in the record matches the current item’s email.  Since we stored the email as a record using the data select we need to retrieve it using JSON notation in the Expressions tab.  The JSON will be for the current loop item ‘PM’ property.  items(‘Apply_to_each’)?[‘PM’]

apply1

Loop Step#2 – Now that we have the records for one of the Project Managers we need to turn them into a table. First we need to use Parse JSON to re-apply a schema to the filtered records so we can access individual columns.  We parse the Body output of the filter array action. Run the flow once after adding the Parse JSON and use the output from the filter array to generate a JSON schema.

apply2

Loop Step#3 – Now we are ready to create the HTML table to embed in the email. Use the Create HTML Table set for custom and map the columns you want in the table from the Parse JSON output. I used the programmer’s displayName, Hours, Status, and Project columns. You may have multiple columns with the same name, like DisplayName.  These represent the embedded properties in objects.  For example the DisplayName might be for the Project Manager, the Programmer, or the person who created the item in the list. You can tell which one you added by hovering over the column and looking at the JSON.

apply3

Loop Step#4 – The last step in the loop is to send the email. Use the same JSON that you used to filter the array in Loop Step#1, since that is the email address of the Project manager. Fill out the subject and Body of the email with whatever information you want, including the HTML table you just created.

Apply4

That’s all you need.  The loop will now process each Person in the list and send one email to each with the correct information. I’m sure your list will be different, but the steps should be essentially the same.

Published by

Paul Papanek Stork

I am a SharePoint MVP who has specialized in Microsoft products since the mid-1990s. As a "Jack of all Trades" I have developed expertise as a network administrator, developer, and DBA. I works as a consultant/trainer where my breadth of knowledge makes me ideally suited to combine Administrative, Development, and SharePoint Designer topics. My 20+ years of experience and broad background make me a much sought after resource for SharePoint questions that cross traditional boundaries. I was a contributing author to the Developer's Guide to Windows SharePoint Services v3 Platform and the SharePoint Server 2007 Deployment Best Practices. My most recent book, the MCTS Windows SharePoint Services 3.0 Configuration Study Guide: Exam 70-631, was released in October, 2009.