How to iterate over the headers in an Azure Logic App? - azure-logic-apps

In a http triggered Logic App I'd like to iterate over the incoming http headers and use only the x-... headers. These headers should be appended to a JSON message.
I know how to access the headers using triggerOutputs()['headers'], how to iterate over collections and how to construct a JSON message using compose. But how to iterate over the headers?
Note: I don't know the x-... header names and I'd like to append any x-... header into the message, I don't want to update the Logic App if a new header is being added or one is removed.
I tried to create a ForEach that iterates over triggerOutputs()['headers'], but that's not a collection.
The header content:
{
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Accept": "*/*",
"Accept-Encoding": "br,gzip,deflate",
"Host": "prod-42.westeurope.logic.azure.com:443",
"x-batch-uuid": "e74f68b9-d69e-4d50-bfe2-65bd6b38dd45",
"x-event": "12361",
"x-sequence": "1",
"x-trigger-id": "sample-trigger-id",
"x-trigger-time": "1672935303",
"x-uuid": "66fa9513-ab42-4f31-90ff-b44582f7d72f",
"Content-Length": "308",
"Content-Type": "application/json"
}
Current version with static x-header copy action
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"MessageBody": {
"inputs": {
"data": "#triggerBody()",
"meta": "#outputs('X-Headers')"
},
"runAfter": {
"X-Headers": [
"Succeeded"
]
},
"type": "Compose"
},
"Send_message": {
"inputs": {
"body": {
"ContentData": "#{base64(outputs('MessageBody'))}"
},
"host": {
"connection": {
"name": "#parameters('$connections')['servicebus']['connectionId']"
}
},
"method": "post",
"path": "/#{encodeURIComponent(encodeURIComponent('schedule-events'))}/messages"
},
"runAfter": {
"MessageBody": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"X-Headers": {
"inputs": {
"x-batch-uuid": "#{triggerOutputs()?['headers']?['x-batch-uuid']}",
"x-event": "#{triggerOutputs()?['headers']?['x-event']}",
"x-sequence": "#{triggerOutputs()?['headers']?['x-sequence']}",
"x-trigger-id": "#{triggerOutputs()?['headers']?['x-trigger-id']}",
"x-trigger-time": "#{triggerOutputs()?['headers']?['x-trigger-time']}",
"x-uuid": "#{triggerOutputs()?['headers']?['x-uuid']}"
},
"runAfter": {},
"type": "Compose"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"schema": {
"properties": {
"person": {
"properties": {
"href": {
"type": "string"
},
"personId": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"servicebus": {
"connectionId": "/subscriptions/.../resourceGroups/.../providers/Microsoft.Web/connections/servicebus-1",
"connectionName": "servicebus-1",
"id": "/subscriptions/.../providers/Microsoft.Web/locations/westeurope/managedApis/servicebus"
}
}
}
}
}

As mentioned by #skin, you cannot iterate for each loop over an object. Headers is an object, and we can iterate for each loop for array only. As a workaround I have tried a below method. Kindly try if it helps in your case.
Created logic app as shown below,
In for each loop, taken condition as shown below
if(startsWith(item(),'-x'),true,false)
Logic app ran successfully as shown below,

Related

How to create an invoice template

Not sure if this is even possible, but what I'm looking to do create an invoice template via some SQL output and send them out as emails.
What I have tried so far is run a piece of SQL that contains all the details of an invoice, parse it as JSON, then create a csv table and lastly send them out via email with a CSV as an attachment.
The issue that I'm facing is that it's pretty basic; I would like to create a template of sorts and later converted to a PDF.
I'm moving away from SSRS and trying to use a logic app for the first time so not sure if this is something even achievable.
Appreciate any tips/help. Thanks in advance.
Regards
One way to achieve this is to use to Create CSV table with Custom Headers from Sql connector Get rows (V2) action and then convert it to Excel using 3rd party connector Plumsail's Csv to Excel action to transform the results of it into pdf using Convert Excel to PDF action of Adobe PDF services Connector.
Below is the flow of my logic app where I receive a sorted csv's pdf in my email attachments.
RESULTS:
TABLE IN SQL DATABASE
RESULT IN LOGIC APP
RESULT IN MAIL
To reproduce the same in your logic app, you can use below code view
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Convert_Excel_to_PDF": {
"inputs": {
"body": {
"$content-type": "multipart/form-data",
"$multipart": [
{
"body": "EXCEL_TO_PDF",
"headers": {
"Content-Disposition": "form-data; name=\"intent\""
}
},
{
"body": "pdfFile.xlsx",
"headers": {
"Content-Disposition": "form-data; name=\"inputFileName\""
}
},
{
"body": "#body('Csv_to_Excel')",
"headers": {
"Content-Disposition": "form-data; name=\"InputFile0\""
}
}
]
},
"headers": {
"x-api-key": "PowerAutomate"
},
"host": {
"connection": {
"name": "#parameters('$connections')['adobepdftools']['connectionId']"
}
},
"method": "post",
"path": "/operation/v1/createPDFFromExcel"
},
"runAfter": {
"Csv_to_Excel": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"Create_CSV_table": {
"inputs": {
"columns": [
{
"header": "BillNo",
"value": "#item()?['billNo']"
},
{
"header": "OrderedDate",
"value": "#item()?['orderedDate']"
},
{
"header": "OrderValue",
"value": "#item()?['orderValue']"
}
],
"format": "CSV",
"from": "#body('Get_rows_(V2)')?['value']"
},
"runAfter": {
"Get_rows_(V2)": [
"Succeeded"
]
},
"type": "Table"
},
"Csv_to_Excel": {
"inputs": {
"body": {
"content": "#{base64(body('Create_CSV_table'))}",
"hasHeaderRecords": true
},
"host": {
"connection": {
"name": "#parameters('$connections')['plumsail_3']['connectionId']"
}
},
"method": "post",
"path": "/flow/v1/Documents/jobs/Csv2Xlsx"
},
"runAfter": {
"Create_CSV_table": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"Get_rows_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['sql']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('default'))},#{encodeURIComponent(encodeURIComponent('default'))}/tables/#{encodeURIComponent(encodeURIComponent('[dbo].[Invoices]'))}/items",
"queries": {
"$orderby": "billNo asc"
}
},
"runAfter": {},
"type": "ApiConnection"
},
"Send_an_email_(V2)": {
"inputs": {
"body": {
"Attachments": [
{
"ContentBytes": "#{body('Convert_Excel_to_PDF')?['fileContent']}",
"Name": "#body('Convert_Excel_to_PDF')?['fileName']"
}
],
"Body": "<p>This is a sample email</p>",
"Importance": "Normal",
"Subject": "Sample",
"To": "<TO_MAIL_ID>"
},
"host": {
"connection": {
"name": "#parameters('$connections')['office365_1']['connectionId']"
}
},
"method": "post",
"path": "/v2/Mail"
},
"runAfter": {
"Convert_Excel_to_PDF": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"evaluatedRecurrence": {
"frequency": "Minute",
"interval": 3
},
"recurrence": {
"frequency": "Minute",
"interval": 3
},
"type": "Recurrence"
}
}
},
"parameters": {
"$connections": {
"value": {
"adobepdftools": {
"connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/adobepdftools-4",
"connectionName": "adobepdftools-4",
"id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/adobepdftools"
},
"office365_1": {
"connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/office365-1",
"connectionName": "office365-1",
"id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/office365"
},
"plumsail_3": {
"connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/plumsail-3",
"connectionName": "plumsail-3",
"id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/plumsail"
},
"sql": {
"connectionId": "/subscriptions/<SUB_ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/sql",
"connectionName": "sql",
"id": "/subscriptions/<SUB_ID>/providers/Microsoft.Web/locations/centralus/managedApis/sql"
}
}
}
}
}

I am able to set up to receive emails, but I am unable to upload attachments to onedrive. Please tell me how to upload the file

I want to store a specific Excel file attached to an email I receive in outlook in a specific folder in onedrive.
(I have the process to store the file in blob, but I want to process the Excel file, so I am thinking of saving it once in onedrive.)
I am able to set up to receive emails, but I am unable to upload attachments to onedrive. Please tell me how to upload the file.
The contents of the Excel file, but there are multiple sheets.
The json looks like this. (partially hidden)
{
"id": "/subscriptions/XXXXXXXX/resourceGroups/XXXXXXXX/providers/Microsoft.Web/sites/test/workflows/excel_test2",
"name": "test/excel_test2",
"type": "Microsoft.Web/sites/workflows",
"kind": "Stateful",
"location": "XXXXXXXX",
"properties": {
"files": {
"workflow.json": {
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_each": {
"actions": {
"Update_file": {
"inputs": {
"body": "#base64ToBinary(items('For_each')?['contentBytes'])",
"host": {
"connection": {
"referenceName": "onedriveforbusiness"
}
},
"method": "put",
"path": "/datasets/default/files/#{encodeURIComponent(encodeURIComponent('filebox\\raw\\*.xlsm'))}"
},
"runAfter": {
"Getting Attachments_(V2)": [
"Succeeded"
]
},
"runtimeConfiguration": {
"contentTransfer": {
"transferMode": "Chunked"
}
},
"type": "ApiConnection"
},
"Getting Attachments_(V2)": {
"inputs": {
"host": {
"connection": {
"referenceName": "office365"
}
},
"method": "get",
"path": "/codeless/v1.0/me/messages/#{encodeURIComponent(triggerBody()?['id'])}/attachments/#{encodeURIComponent(items('For_each')?['id'])}"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"foreach": "#triggerBody()?['attachments']",
"runAfter": {},
"type": "Foreach"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"triggers": {
"When you receive a new email_(V3)": {
"inputs": {
"fetch": {
"method": "get",
"pathTemplate": {
"template": "/v3/Mail/OnNewEmail"
},
"queries": {
"fetchOnlyWithAttachment": true,
"folderPath": "Id::XXXXXXXX=",
"importance": "Any",
"includeAttachments": true
}
},
"host": {
"connection": {
"referenceName": "office365"
}
},
"subscribe": {
"body": {
"NotificationUrl": "#{listCallbackUrl()}"
},
"method": "post",
"pathTemplate": {
"template": "/GraphMailSubscriptionPoke/$subscriptions"
},
"queries": {
"fetchOnlyWithAttachment": true,
"folderPath": "Id::XXXXXXXX=",
"importance": "Any"
}
}
},
"metadata": {
"Id::XXXXXXXX=": "XXXXXXXX_1"
},
"splitOn": "#triggerBody()?['value']",
"type": "ApiConnectionNotification"
}
}
},
"kind": "Stateful"
}
},
"flowState": 2,
"health": {
"state": 1
}
}
}
One way to achieve your requirement is to create a file in OneDrive with the same name as you are receiving from outlook. Below is the flow of my logic app.
Below is the mail I'm receiving in my outlook
RESULTS:
Results in Logic Apps
Results in One drive

Azure Logic apps - output array in email body

I have trouble using my array variable in my V2 email in Logic apps. I would like to output my array in the email body. For example if my array is ["1","2",3"]. Im want each value on its own row.
1
2
3
I have 2 issues, outputting my array in the email body(only strings showing in the dynamic menu) and making each value on a row.
After reproducing from my end, I could able to achieve your requirement by replacing "," with "< br>< br>" while sending the email using Send an email (V2) action. Below is the expression that worked for me.
replace(string(variables('SampleArray')),',','<br><br>')
Here is the flow of my Logic App
Result in my Inbox:
To reproduce the same, you can use below codeview in your logicapp
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"SampleArray": {
"inputs": {
"variables": [
{
"name": "SampleArray",
"type": "array",
"value": [
1,
2,
3
]
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"Send_an_email_(V2)": {
"inputs": {
"body": {
"Body": "<p>#{replace(string(variables('SampleArray')),',','<br><br>')}</p>",
"Importance": "Normal",
"Subject": "Sample",
"To": "<YOUR-EMAIL>"
},
"host": {
"connection": {
"name": "#parameters('$connections')['office365']['connectionId']"
}
},
"method": "post",
"path": "/v2/Mail"
},
"operationOptions": "DisableAutomaticDecompression",
"runAfter": {
"SampleArray": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"office365": {
"connectionId": "/subscriptions/<SUB-ID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/office365",
"connectionName": "office365",
"id": "/subscriptions/<SUB-ID>/providers/Microsoft.Web/locations/centralus/managedApis/office365"
}
}
}
}
}

How to display array in Slack Webhook of Azure Logic Apps?

I have LogicApps. It receives property ListOfNames array in HTTP request.
I have difficulty to display correctly in Slack webhook. How to format body?
Current message displayed in Slack:
ListOfNames:
["Name1 \n","Name2\n","Name3\n"]
Target message in slack:
ListOfNames:
Name1
Name2
Name3
I tried Split:
Code: No errors, but don't array items listed correctly in message:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": "#split(string(triggerBody()?['ListOfTesNames']),'\\n') ",
"runAfter": {},
"type": "Compose"
},
"HTTP_Webhook": {
"inputs": {
"subscribe": {
"body": {
"blocks": [
{
"text": {
"text": "*[dev] Testing:* \n ListOfTestNames #{outputs('Compose')} ",
"type": "mrkdwn"
},
"type": "section"
},
{
"text": {
"text": "Please check test results from database ",
"type": "mrkdwn"
},
"type": "section"
}
]
},
"method": "POST",
"uri": "https://hooks.slack.com/services/1111111111111111111111"
},
"unsubscribe": {}
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"runtimeConfiguration": {
"staticResult": {
"name": "HTTP_Webhook0",
"staticResultOptions": "Disabled"
}
},
"type": "HttpWebhook"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"staticResults": {
"HTTP_Webhook0": {
"outputs": {
"headers": {},
"statusCode": "OK"
},
"status": "Succeeded"
}
},
"triggers": {
"manual": {
"inputs": {
"method": "POST",
"schema": {
"properties": {
"ListOfTestNames": {
"type": "array"
}
},
"type": "object"
}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
You can add a compose connector before the webhook connector and use split functionality which gives the right format and then add the output of the compose connector to your webhook connector.
Here is a screenshot for your reference:
Note: Make sure you modify the expression in your split from compose connector as split(triggerBody()?['ListOfFiles'],'\n') to split(triggerBody()?['ListOfFiles'],'\n') because when we write the expression for this, it first takes '\n' as a string and then adds another ''.
Below is the split expression in compose connector:
split(triggerBody()?['ListOfFiles'],'
')
Updated Answer
As we are retrieving an array and passing it to the body of text it is adding the extra characters to it. We have created a new Logic App and added each item that we are retrieving to a variable and finally added that variable to the Webhook body.
Here is the screenshot of the logic app
Here is my code view
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_each": {
"actions": {
"For_each_2": {
"actions": {
"Append_to_string_variable": {
"inputs": {
"name": "ListOfFilesFinal",
"value": "#{items('For_each_2')},"
},
"runAfter": {},
"type": "AppendToStringVariable"
}
},
"foreach": "#split(item(),',')",
"runAfter": {},
"type": "Foreach"
}
},
"foreach": "#triggerBody()?['ListOfFiles']",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "Foreach"
},
"HTTP_Webhook": {
"inputs": {
"subscribe": {
"body": {
"blocks": [
{
"text": {
"text": "*[dev] Testing: * \n ListOfTestNames : #{variables('ListOfFilesFinal')} ",
"type": "mrkdwn"
},
"type": "section"
},
{
"text": {
"text": "Please check test results from database ",
"type": "mrkdwn"
},
"type": "section"
}
]
},
"method": "POST",
"uri": "#listCallbackUrl()"
},
"unsubscribe": {}
},
"runAfter": {
"For_each": [
"Succeeded"
]
},
"runtimeConfiguration": {
"staticResult": {
"name": "HTTP_Webhook0",
"staticResultOptions": "Disabled"
}
},
"type": "HttpWebhook"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "ListOfFilesFinal",
"type": "string"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"staticResults": {
"HTTP_Webhook0": {
"outputs": {
"headers": {},
"statusCode": "OK"
},
"status": "Succeeded"
}
},
"triggers": {
"manual": {
"inputs": {
"method": "POST",
"schema": {
"properties": {
"ListOfFiles": {
"type": "array"
}
},
"type": "object"
}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}

Can I chain multiple actions for every item in a repeat list

tried a simple logic app where I was able to retrieve a list from an API. Using the repeat over a list feature, I was able to send an email for every item in the list.
But I really want to be able to perform several chained actions/steps for every item in the list...isn't that possible? I know I can have multiple actions/steps that perform something for each item in the same list...but these are not chained as in the following code:
"triggers": {
"recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 1
},
"type": "Recurrence"
}
},
"actions": {
"http": {
"type": "Http",
"inputs": {
"method": "GET",
"uri": "https://example.com/pcme/3/7",
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic my auth"
}
},
"conditions": []
},
"office365connector": {
"type": "ApiApp",
"inputs": {
"apiVersion": "2015-01-14",
"host": {
"id": "/subscriptions/xxxx/resourcegroups/workflows/providers/Microsoft.AppService/apiapps/office365connector",
"gateway": "https://workflowsxxxxxx.azurewebsites.net"
},
"operation": "SendMail",
"parameters": {
"message": {
"To": "some-email#me.com",
"Subject": "#repeatItem().activationCode"
}
},
"authentication": {
"type": "Raw",
"scheme": "Zumo",
"parameter": "#parameters('/subscriptions/xxxxxx/resourcegroups/workflows/providers/Microsoft.AppService/apiapps/office365connector/token')"
}
},
"repeat": "#body('http')",
"conditions": [
{
"expression": "#equals(actions('http').status, 'Succeeded')"
}
]
},
"office365connector0": {
"type": "ApiApp",
"inputs": {
"apiVersion": "2015-01-14",
"host": {
"id": "/subscriptions/xxxx/resourcegroups/workflows/providers/Microsoft.AppService/apiapps/office365connector",
"gateway": "https://workflowsdxxxx.azurewebsites.net"
},
"operation": "SendMail",
"parameters": {
"message": {
"To": "some-email#gmail.com",
"Subject": "#repeatItem().cardNumber"
}
},
"authentication": {
"type": "Raw",
"scheme": "Zumo",
"parameter": "#parameters('/subscriptions/xxxxx/resourcegroups/workflows/providers/Microsoft.AppService/apiapps/office365connector/token')"
}
},
"repeat": "#body('http')",
"conditions": [
{
"expression": "#equals(actions('http').status, 'Succeeded')"
}
]
}
Thank you for any help.
Regards
One option for chaining actions over each item in the list is to use a nested logic app.
The way you would set it up is to have a child logic app with a chain of actions that you want to apply to each individual item. The parent logic app would then use the workflow action type in order to invoke a run of the child logic app each of your repeat items.
Your parent workflow would then be defined as
"actions": {
"http": {
"type": "Http",
"inputs": {
"method": "GET",
"uri": "https://example.com/pcme/3/7",
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic my auth"
}
},
"conditions": []
},
"processEachItem" : {
"type": "workflow",
"inputs": {
"uri": <child flow direct invoke uri>,
"apiVersion": "2015-02-01-preview",
"trigger": {
"name" : "runNow",
"outputs": { "item": "#repeatItem()" }
},
"authentication": {
"type" : " Basic",
"username" : "myKey",
"password" : "xxxxxxxxxxxxxxx",
},
"repeat": "#body('http')",
}
}
}
The following blog post explains the details on how to use nested workflows (how to obtain the direct invoke URI and configure authentication) and has a nice sample: https://blogs.msdn.microsoft.com/carlosag/2015/05/31/using-nested-azure-logic-apps-or-invoking-flows-from-another-logic-app/

Resources