Darryl
Darryl

Reputation: 23

How to use compose (or another function?) to add Forms data to an email in Power Automate?

I have information that I've collected in a Microsoft Form, and I want to use Power Automate to send that in an email.

The form data will be something like this (FieldName: Value):

CorporateSite: Yes

CorporateNews: Yes

CorporateNewsTitle: This Is Our News Story

CorporateBannerEnd: null

CorporatePage: No

CorporatePageURL: null

I don't want to include the null or "no" fields, so in this example, my desired output would be:

CorporateSite: Yes

CorporateNews: Yes

CorporateNewsTitle: This Is Our News Story

I know how to get the form results into Power Automate, and also how to send the email, but I'm trying to find a non-clunky way to have clean email content when many of the fields may be empty.

There are three main sections to the form data, each with a yes/no question indicating if there will be content in that section. So, it's easy to use a condition for each of the three sections to only include content from that section if the user chose "yes" (the example above shows some questions from the "Corporate Site" section of the form).

When my condition is "yes", I want to run through the fields in that section and collect the ones that have content, as illustrated in my desired output, above. I think that something like this will work to test each field and include the value if there is one:

if(empty(outputs('Get_response_details')?['body/r008aa103d6044ae19833da19494676e0']), null, outputs('Get_response_details')?['body/r008aa103d6044ae19833da19494676e0'])

I could just use a whole bunch of those "if" expressions in the email (one for each form field), but it seems like it would be more efficient to only run through the Corporate ones, for example, if the Corporate condition is met:

Control

I'm not sure, though, what control to use to "hold" those if statements for each field and then get me the output I need to include in the email. I tried using a text variable, but when I just pasted the code above in, it came back verbatim (the email included the code, not the output of the code). When I tried including the above as a function, there was no output.

So, how can I get the suitable fields to display a lines of text in my email?

EDIT/UPDATE: Here is what the form response JSON looks like.

I don't want the email to include field with "" as the response, as well as some of the others that are just used to facilitate navigation through the form, but don't provide any information relevant to fulfilling the request.

If it would help, I could adjust the form response choices so that only fields which shouldn't be included in the email have answers starting with either "Yes" or "No".

"body": {
    "responder": "zzz",
    "submitDate": "1/13/2025 4:55:58 PM",
    "r22a00717441f472b885badc71fdb2de1": "None of the above, and no meeting is required",
    "r33a4f1266d9b4ef2858148f7c370f517": "No",
    "rc254ffe7f8d447978c36c610d9f89cf3": "2025-01-16",
    "rc533ab68aaf745e98d9ac19c968a420f": "Sample Request",
    "r64e954319c6b4326b7f11e7edcabf514": "Webel publishes and informs the Advisor",
    "r88fb7e09975742a49901b9537586598e": "Sample Story Request",
    "r31b78ff0ab9640498942ed4f3ad8d162": "You'll love this sample news story, full of words and a picture",
    "r4aaa4e35d4aa4a16b2fd1172604cac5b": "a group of happy students",
    "rf5560b79448949fd9dd44993d03a45d7": "John Doe",
    "r3b94cc1d1d0b45b38717fb3c5392f507": "",
    "r198cf5797bfa4a719b6e243a4a768854": "",
    "rc8f3d7483ba7447d9e36350fa94e4e50": "",
    "r37d92ffffc5c4f4db03deaf10eaf72d8": "",
    "r75aa2e1c4d6d4df3a692b49ef9cfe541": "sample.url/page",
    "r6fe1c5aed7c54e8f9b49f33d1f9edfef": "Please replace the graphic with the one requested for the news story",
    "rb940f08155c045b1af24efd8d0c3cd54": "",
    "r3a20300a86ba42a884b81c03013d58f7": "",
    "r1cd2338d8da04e2b8f068b9f5c53843d": "",
    "r799a1b00b9304cd0afb80ab49f71d0a2": "Non-banner",
    "rad4ba23c35db44859d9603ebcf75993d": "",
    "r7ae3c6aab9b3438cb705d4f13d073007": "",
    "r57ba068b40a04d3d9e99c64940cbd44a": "",
    "r9ee7489c59a9431681ef4dbfd3c53ff2": "Yes",
    "r49e6c28f45a54ec9a176931aa638d7a0": "Yes",
    "rc943baef494a4ba3a870c62acdde3068": "Yes",
    "r4eacc1bafa124dd184475e21855b96ab": "",
    "r74214995486d4dfe9480426aaeb71e0f": "Yes, other corporate site components",
    "rcd630471ec1a48fcb5d0c5acbdc77f7d": "",
    "r79c0eed8f1b64ad8ae4a7faffe219a59": "",
    "r5dd7226f71da4369a1acca8fad9d5651": "Yes, but not for the corporate site",
    "rb37f681768ab4e78a2e9b65fbe2c979f": "",
    "rc8e1feae50db429db8815aa8f5bd959e": "",
    "r787c00f85cc94f3891e9a16594aff0b5": "",
    "r156ad4e9e15f4de896eb318e156d4935": "",
    "r0e3ebf63dbc349efb02d7b36c1a501be": "",
    "r85490e9748a14ec0bfb45e5e6333bc4d": "",
    "r05627f01fadd4bbdbff5b969afc543d1": "",
    "r9255e6818106430b9a7886387b51c8de": "",
    "r46931e1013d848498751ec59d7227407": "",
    "r53582d2a762b41659ec8644c08453c28": "",
    "r193c6b1dc8d944859375c5df545cd748": "",
    "r086468a6f7394fee9652a7f4a08b90aa": "",
    "rd504c0bc5c0a470d8aa915b9163ea911": "",
    "r7aebff3258894c0cbbd789d96c071fb9": "",
    "rfd08983ad3ef4d5298362cc57719969f": "",
    "r1f1ecf42d3fe4e6881750d6dd686495e": "",
    "re098869c7820431aae68d0f4ab100986": "",
    "rebdb9f700e3642ac992fbe3e081fdb25": "",
    "ra8b9a502cd3947f084056726beb3bfb1": "",
    "rd4fc40f0d261457a88c9a8e3c387edeb": "",
    "r42d96cc8ac9d41f3a626564445b4e879": "",
    "r8d9734799eb141b9b3b84aa8cb986c87": "",
    "rb7b59df9c1974facb4e4a216a21a9dad": "",
    "rc761d35862f84286b27f5c1d947b7bf5": "",
    "rd79fc1d0ee774e7ab5b8efe7db8f3981": "",
    "r0e60d284e88042caaf973300ccf5c6f0": "",
    "r717fef2877f043488b5ff14aee5d94ac": "",
    "r1f59a6efe9144313be397d9474c2cdc6": "",
    "r1d1eb6f163014ad5b84727a2194c7b39": "",
    "r226502c0a9274c5c9044d7fc54382c7a": "",
    "r3342647606944b5d9bb3bc05f055e9d8": "No",
    "rd46db32581a0426092e674a133f10036": "Yes",
    "r230f5d84bf204ed8ae44ae2475d31dba": "",
    "r4ab1cab232644bbc9935de7d9eb1c477": "",
    "r25fd6d077f734204ba67aba0a2dd8b22": "",
    "rb515c1a1b9ac4719aa3e45a431119c22": "",
    "ra9da7c2158ee430ea221a403c20423db": "",
    "r008aa103d6044ae19833da19494676e0": "",
    "rcde6dd1823d74ca09cae59184682db6a": "",
    "r764f4a7f630d47c69e9fa5e68844bb97": "",
    "re62886ed4dad480f8a5e507ace836ec3": "No",
    "r070626538cb442c69b312bf063a3ff72": "",
    "r8ddddcfa436949efa95085e9ef4c1cd1": "",
    "re341fcc8e0bd484f84de51d4e69e97e0": "",
    "r423b69e132e143ebb979b6c884bae1ba": "Yes",
    "r25c480ae73174d0eb1f80d620858c052": "Modify an existing document",
    "re6d39c588f8e46c19e09973aceba5447": "sample.url/document",
    "r691a2307cd2941beaa88c4152dbcbb08": "Please change the heading to H1",
    "r9046b9ccc38b4bcc923edcfe635cdcd2": "No other components to my request",
    "r9fee67532a8a42beb6c9634899dfda8b": "",
    "r69ced8e70d37441b83a2e0d443179e6b": "",
    "r9a462f887c154e7dab111d74c6b02a20": "",
    "rc79ee7c66da94c549d40f68d91cb6977": "",
    "r17e3746959e7412cac184b6cc0737083": "",
    "rd1eb7be6ff734695ba0ddd7de7f50d06": "",
    "r5afec204c3e54f58b390092e692ef121": "",
    "reee1cdfd97ad4777aed5437ea33b12c6": "",
    "rfaa2e592fb954054b0966853f1804999": "",
    "r737c3cd0d40847ab87bdc92e0bfed779": "No other components, take me to the last question",
    "r26436c57b5254e529586ba91d531fb55": "",
    "r1fc329cff7274ed8abb247831bc119d4": "",
    "re4be7860dbfb4a6b9ffd0c6960fb7f06": "",
    "r1a3e29e151884e67a1cc61adce470011": "",
    "ra426896adcef47d4970512d50f62e347": "",
    "r4c8bc1fa8d6e49f7ba81d27784417c41": "",
    "r22a572bac6d84e79a27c2f59d17be4d5": "",
    "r7cc8f340f5e64ff7be556940eda4986c": "No",
    "rb192b97df860450ba280e4306732b3c8": "",
    "rdeea55f9c47b489babbb762f9fd51948": "No preference"
}

}

EDIT/UPDATE:

Here's the progress so far based on guidance from @sam-nseir.

Trigger is when a new Forms response is submitted; after that, I initialize two variables (one to determine which inbox to send the request to, one to determine whether to mark the request as urgent):

trigger and variables

Next I have my compose function to create the array for my form responses, with the form item ID and label for each:

compose array

Next I have my get response details function to pull the form info in, and the select function to feed those form values into the array initialized in the compose, above:

get response and select

Label value is @item()?['label'] Response value is outputs('Get_response_details')?['body']?[item()?['id']]

The select function is where the flow is failing though, generating this error:

flow error

This may be because the inputs and outputs of the Compose aQuestions control are identical: aQuestions results

Upvotes: 0

Views: 138

Answers (1)

Sam Nseir
Sam Nseir

Reputation: 12111

This solution essentially converts the response from an object to an array of questions/answers, which can then be filtered, and ultimately converted to an HTML table to add into your email.


First, you need an array to hold the question IDs and their labels. This step you will need to complete manually and do so in the order you want to show in your email. Add a Compose action and name it aQuestions, preferably before your Get response details step.

[
  { "id": "r008aa103d6044ae19833da19494676e0", "label": "Corporate Site" },
  { "id": "rcde6dd1823d74ca09cae59184682db6a", "label": "Label Alpha" },
  { "id": "r764f4a7f630d47c69e9fa5e68844bb97", "label": "Label Bravo" },
  { "id": "re62886ed4dad480f8a5e507ace836ec3", "label": "Label Charlie" },
  { "id": "r070626538cb442c69b312bf063a3ff72", "label": "Label Delta" }
]

This is just a demo of the array structure, you will have many more records etc... Take care for the last one not to have a comma at the end. This is a JSON Array of records (objects).

aQuestions

If you are willing and able, you can replace the above and instead get the IDs and Labels from the Form REST API. See Get Question details of Forms with Power Automate and also see this reddit answer for using HTTP with Microsoft Entra ID instead of the now not allowed HTTP Request to SharePoint


Next, after your Get response details step, add a Select action (under Data Operation), and rename it to aResponses.

From: outputs('aQuestions')
Map:
  Question: item()?['label']
  Response: outputs('Get_response_details')?['body']?[item()?['id']]

It should look like: aResponses


Next, add a Filter array action (under Data Operation), and rename it to aResponsesFiltered, with the following:

From: @body('aResponses')
Filter: @and(not(equals(item()?['Response'], 'No')), not(equals(item()?['Response'], '')))

Add the filter via Edit in advance mode. It should look like: aResponsesFiltered This removes responses that are either "" or "No".

You can also use this alternative filter:

@not(contains(item()?['Response'], array('No', '')))

Next, add a Create HTML table (under Data Operation) with:

From: body('aResponsesFiltered')
Columns: Automatic

HTML Table

And finally, add the output of this step into your email.


Here is my mock-up for demo purposes only (my Get response details is a mimic of yours).

Edit mode

And the result/output: Response output

Upvotes: 0

Related Questions