skip to Main Content

I’m trying to add a dynamic distribution list to an email I send out with a logic app. It’s triggered whenever a blob is uploaded into a container it is watching.

Is it possible to:

  1. grab a value from the csv and enter it into the email that is being composed.
  2. Is it possible to exclude this column when it is being attached in the email?

For example if the csv were to be composed like so:

+----+---------+---------+---------+--------------------+
| ID | Column1 | Column2 | Column3 | Distribution List  |
+----+---------+---------+---------+--------------------+
| 1  | 500     | R       | 500     | [email protected] |
+----+---------+---------+---------+--------------------+
| 2  | 600     | R       | 600     | [email protected] |
+----+---------+---------+---------+--------------------+
| 3  | 700     | DED     | NULL    | [email protected] |
+----+---------+---------+---------+--------------------+

I need the value in the distribution list column to go here:
enter image description here

The distribution list would only be there for the purpose of building the email, is there a way to remove it before the blob content is attached?

2

Answers


  1. I have used given workflow to pass the distribution list name in the email and then removed the column before attaching it to the email.

    enter image description here
    enter image description here

    I have used below JavaScript code to parse the csv data to JSON.

    Parsed the JSON array and then used Select action to fetch the required columns and lastly used Create CSV table action to form a CSV table which will be used as an email attachment.

    const csvData = workflowContext.actions.Get_blob_content_V2.outputs.body; // Blob content as a string
    
    
    const lines = csvData.trim().split('n');
    const result = [];
    
    if (lines.length === 0) {
        return result; 
    }
    
    const headers = lines[0].split(',').map(header => header.trim());
    
    for (let i = 1; i < lines.length; i++) {
        const obj = {};
        const currentLine = lines[i].split(',').map(field => field.trim());
    
        for (let j = 0; j < headers.length; j++) {
            obj[headers[j]] = currentLine[j];
        }
        result.push(obj);
    }
    
    return result;
    

    Code

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Create_CSV_table": {
                    "inputs": {
                        "format": "CSV",
                        "from": "@body('Select')"
                    },
                    "runAfter": {
                        "Select": [
                            "Succeeded"
                        ]
                    },
                    "type": "Table"
                },
                "Execute_JavaScript_Code": {
                    "inputs": {
                        "code": "const csvData = workflowContext.actions.Get_blob_content_V2.outputs.body; // Blob content as a stringrnrnrnconst lines = csvData.trim().split('\n');rnconst result = [];rnrnif (lines.length === 0) {rn    return result; rn}rnrnconst headers = lines[0].split(',').map(header => header.trim());rnrnfor (let i = 1; i < lines.length; i++) {rn    const obj = {};rn    const currentLine = lines[i].split(',').map(field => field.trim());rnrn    for (let j = 0; j < headers.length; j++) {rn        obj[headers[j]] = currentLine[j];rn    }rn    result.push(obj);rn}rnrnreturn result;rn"
                    },
                    "runAfter": {
                        "Get_blob_content_V2": [
                            "Succeeded"
                        ]
                    },
                    "type": "JavaScriptCode"
                },
                "For_each": {
                    "actions": {
                        "Send_an_email_(V2)": {
                            "inputs": {
                                "body": {
                                    "Attachments": [
                                        {
                                            "ContentBytes": "@{base64(body('Create_CSV_table'))}",
                                            "Name": "@triggerBody()?['Name']"
                                        }
                                    ],
                                    "Body": "<p class="editor-paragraph">Test Email.....</p>",
                                    "Importance": "Normal",
                                    "Subject": "CSV file",
                                    "To": "@items('For_each')['Distribution List']"
                                },
                                "host": {
                                    "connection": {
                                        "name": "@parameters('$connections')['outlook']['connectionId']"
                                    }
                                },
                                "method": "post",
                                "path": "/v2/Mail"
                            },
                            "type": "ApiConnection"
                        }
                    },
                    "foreach": "@outputs('Parse_JSON')['body']",
                    "runAfter": {
                        "Create_CSV_table": [
                            "Succeeded"
                        ]
                    },
                    "type": "Foreach"
                },
                "Get_blob_content_V2": {
                    "inputs": {
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['azureblob']['connectionId']"
                            }
                        },
                        "method": "get",
                        "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/@{encodeURIComponent(encodeURIComponent(triggerBody()?['Path']))}/content",
                        "queries": {
                            "inferContentType": true
                        }
                    },
                    "metadata": {
                        "JTJmdGVzdCUyZlRlc3QxMi5jc3Y=": "/test/Test12.csv"
                    },
                    "runAfter": {},
                    "type": "ApiConnection"
                },
                "Parse_JSON": {
                    "inputs": {
                        "content": "@outputs('Execute_JavaScript_Code')?['body']",
                        "schema": {
                            "items": {
                                "properties": {
                                    "Column1": {
                                        "type": "string"
                                    },
                                    "Column2": {
                                        "type": "string"
                                    },
                                    "Column3": {
                                        "type": "string"
                                    },
                                    "Distribution List": {
                                        "type": "string"
                                    },
                                    "ID": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "ID",
                                    "Column1",
                                    "Column2",
                                    "Column3",
                                    "Distribution List"
                                ],
                                "type": "object"
                            },
                            "type": "array"
                        }
                    },
                    "runAfter": {
                        "Execute_JavaScript_Code": [
                            "Succeeded"
                        ]
                    },
                    "type": "ParseJson"
                },
                "Select": {
                    "inputs": {
                        "from": "@outputs('Execute_JavaScript_Code')?['body']",
                        "select": {
                            "Column1": "@item()?['Column1']",
                            "Column2": "@item()?['Column2']",
                            "Column3": "@item()?['Column3']",
                            "ID": "@item()?['ID']"
                        }
                    },
                    "runAfter": {
                        "Parse_JSON": [
                            "Succeeded"
                        ]
                    },
                    "type": "Select"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "When_a_blob_is_added_or_modified_(properties_only)_(V2)": {
                    "evaluatedRecurrence": {
                        "frequency": "Second",
                        "interval": 10
                    },
                    "inputs": {
                        "host": {
                            "connection": {
                                "name": "@parameters('$connections')['azureblob']['connectionId']"
                            }
                        },
                        "method": "get",
                        "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/triggers/batch/onupdatedfile",
                        "queries": {
                            "checkBothCreatedAndModifiedDateTime": false,
                            "folderId": "JTJmdGVzdA==",
                            "maxFileCount": 10
                        }
                    },
                    "metadata": {
                        "JTJmdGVzdA==": "/test"
                    },
                    "recurrence": {
                        "frequency": "Second",
                        "interval": 10
                    },
                    "splitOn": "@triggerBody()",
                    "type": "ApiConnection"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {
                    "azureblob": {
                        "connectionId": "/subscriptions/0e8****b5e7c/resourceGroups/*****/providers/Microsoft.Web/connections/azureblob",
                        "connectionName": "azureblob",
                        "id": "/subscriptions/0e8*******5e7c/providers/Microsoft.Web/locations/northcentralus/managedApis/azureblob"
                    },
                    "outlook": {
                        "connectionId": "/subscriptions/0e8****b5e7c/resourceGroups/*****/providers/Microsoft.Web/connections/outlook",
                        "connectionName": "outlook",
                        "id": "/subscriptions/0e89*******8b5e7c/providers/Microsoft.Web/locations/northcentralus/managedApis/outlook"
                    }
                }
            }
        }
    }
    

    This gives me expected response.

    enter image description here

    enter image description here

    enter image description here

    Login or Signup to reply.
  2. Yes,

    It’s a long question but yu can achieve all of it.

    1. You can fetch email from CSV and add it to the email. You just need to Parse it to a JSON first. You can use this sample strategy.

    enter image description here

    enter image description here

    2. You can exclude this column when attaching to email by creating a parsed CSV. You can do it by this strategy. Pass your JSON of qualifying values to this connector. The output can be attached to the email as File Content.

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search