How to extract URL link from email body using Azure Logic Apps
I have been trying to figure this one out for over last three weeks, and I asked/posted question to the Azure Logic Apps community, but I did not get the solution yet.
So, I am posting here again.
This is summary of my previous post.
Here was my first attempt by following/reading this post:
So, it appears my pipeline passed until 2nd step (Get emails (V3)), but when it hit the JavaScript Code area, it says "Cannot read property 'body' of undefined".
- I got a suggestion to try "var text = workflowContext.actions.Get_emails_(V3).outputsbody.
- When I tried that, I got an error saying "Get_emails_' referenced by 'inputs' in action 'Execute_JavaScript_Code' are not defined in the template.'"
- I got a next suggestion to use Regex code like this (on the bottom).
- Also, I got a suggestion to use "When a new email arrives (V3)" instead of "Get emails (V3)" for the second step.
But, I was not able to find "When a new email arrives (V3)" from my Azure Logic App options.
- I tried with the Regex code, and I got this error ('Cannot read property 'body' of undefined'):
How do I fix to error "Cannot read property 'body' of undefined"?
Thank you!
Azure Logic Apps
-
SwathiDhanwada-MSFT 18,771 Reputation points
2024-06-14T07:36:29.28+00:00 @King Java Thanks for reaching out. I am checking into it and will provide an update soon.
-
SwathiDhanwada-MSFT 18,771 Reputation points
2024-06-18T09:41:18+00:00 @King Java Based on the information provided, I have tested your scenario from my end. Here are the respective details.
- you have mentioned the format of the string to be as shown below.
- I have sent an email with link directly for testing purpose in below format.
- In my scenario, whenever a mail is sent to folder, the logic app automatically triggers.
- From the output of the "When a new email arrives (V3)" action, I have written below code to verify the regex match.
For regex creation , I have used third party tool https://regex101.com/. In similar pattern, based on your requirement, you can create the regex pattern. Here is my sample logic app code for your reference. Kindly customize it based on your requirement.const jsonString = workflowContext.trigger.outputs.body.bodyPreview; const jsonObject = JSON.parse(JSON.stringify(jsonString)) const regex = /originalsrc=\\"https:\/\/portal\.carefeed\.com\/report\/emailReport\?details=[0-9a-fA-F-]{36}\\"/; // Perform the match const match = jsonObject.match(regex); // Check if there was a match and print it if (match) { return "Matched" } else { return "No Match" }
{ "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "Execute_JavaScript_Code": { "inputs": { "code": "\r\n\r\nconst jsonString = workflowContext.trigger.outputs.body.bodyPreview;\r\nconst jsonObject = JSON.parse(JSON.stringify(jsonString))\r\nconst regex = /originalsrc=\\\\\"https:\\/\\/portal\\.carefeed\\.com\\/report\\/emailReport\\?details=[0-9a-fA-F-]{36}\\\\\"/;\r\n\r\n// Perform the match\r\nconst match = jsonObject.match(regex);\r\n\r\n// Check if there was a match and print it\r\nif (match) {\r\n return \"Matched\"\r\n} else {\r\n return \"No Match\"\r\n}\r\n \r\n\r\n" }, "runAfter": {}, "type": "JavaScriptCode" } }, "contentVersion": "1.0.0.0", "outputs": {}, "parameters": { "$connections": { "defaultValue": {}, "type": "Object" } }, "triggers": { "When_a_new_email_arrives_(V3)": { "inputs": { "fetch": { "method": "get", "pathTemplate": { "template": "/v3/Mail/OnNewEmail" }, "queries": { "fetchOnlyWithAttachment": false, "folderPath": "Id::AAMkADExZTRjNDJmLTMwNzktNGM4ZC05YThmLTQ5NWMwODk0ZGQxMwAuAAAAAABrSNp1lgosTJyDa_IAMdNMAQAMlk7ChMbeS7yA0c_URndrAAOD5QzHAAA=", "importance": "Any", "includeAttachments": false } }, "host": { "connection": { "name": "@parameters('$connections')['office365']['connectionId']" } }, "subscribe": { "body": { "NotificationUrl": "@{listCallbackUrl()}" }, "method": "post", "pathTemplate": { "template": "/GraphMailSubscriptionPoke/$subscriptions" }, "queries": { "fetchOnlyWithAttachment": false, "folderPath": "Id::AAMkADExZTRjNDJmLTMwNzktNGM4ZC05YThmLTQ5NWMwODk0ZGQxMwAuAAAAAABrSNp1lgosTJyDa_IAMdNMAQAMlk7ChMbeS7yA0c_URndrAAOD5QzHAAA=", "importance": "Any" } } }, "metadata": { "Id::AAMkADExZTRjNDJmLTMwNzktNGM4ZC05YThmLTQ5NWMwODk0ZGQxMwAuAAAAAABrSNp1lgosTJyDa_IAMdNMAQAMlk7ChMbeS7yA0c_URndrAAOD5QzHAAA=": "Azure SDK" }, "splitOn": "@triggerBody()?['value']", "type": "ApiConnectionNotification" } } }, "parameters": { "$connections": { "value": { "office365": { "connectionId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/Integration/providers/Microsoft.Web/connections/office365", "connectionName": "office365", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Web/locations/eastus/managedApis/office365" } } } } }
-
King Java 500 Reputation points
2024-06-18T22:59:23.06+00:00 I am getting this error.
I think this happens because I have another trigger "Recurrence" before "When a new email arrives (V3)".
Is it right?
-
SwathiDhanwada-MSFT 18,771 Reputation points
2024-06-19T03:57:12.8733333+00:00 @King Java Have you used any other triggers other than "'When a new email arrives (V3)" trigger?
-
King Java 500 Reputation points
2024-06-19T04:53:26.63+00:00 @SwathiDhanwada-MSFT Only trigger that I used was "Recurrence". Please see my other comment on top of yours as I was trying to save earlier and it just got saved.
-
SwathiDhanwada-MSFT 18,771 Reputation points
2024-06-19T05:07:10.02+00:00 @King Java The error has happened due to multiple triggers. You can remove recurrence trigger and just use "When a new email arrives (V3)" trigger. When you get new email , the logic app automatically triggers.
-
King Java 500 Reputation points
2024-06-19T16:57:33.29+00:00 @SwathiDhanwada-MSFT The Logic App went without error for the first time, but there is no match.
I am not sure whether Regex is the issue.
Can you please verify this URL again? I modified little bit for confidentiality.
Bottom is link:
originalsrc="https://portal.carefeed.com/test/reports/report/emailReport?details=c8a7fe26-4055-4f1e-9a6f-b318266c05fa"
Bottom is Regex that I put it inside the code:
const regex = /originalsrc=\\"https:\/\/portal\.carefeed\.com\/test\/reports\/report\/emailReport\?details=[0-9a-fA-F-]{36}\\"/;
-
SwathiDhanwada-MSFT 18,771 Reputation points
2024-06-20T05:30:14.2333333+00:00 @King Java You can verify whether the regex is correct or not using https://regex101.com/.
Indeed the url link and regex provided by you is not matched. Here is screenshot for the same.
Here is modified regex for the same where its matched.
Modified regex :
originalsrc="https://portal.carefeed.com/test/reports/report/emailReport?details=[a-zA-z0-9-]{36}"
-
King Java 500 Reputation points
2024-06-21T17:57:25.2933333+00:00 I modified the Regex last night, but still there is no output.
I think the URL portion to retrieve is wrong.
If I copied and pasted the email message to HTML, I get different URL output.
Bottom is part of email that has URL link:
Bottom is HTML code where top portion is indicated (after I convert from Email to HTML):
Click the link below to view your Admissions/Readmissions Report.
<a data-auth="Verified" rel="noopener noreferrer"
href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fportal.carefeed.com%2FTest%2Freports%2Freport%2FemailReport%3Fdetails%3D8d7d1e90-6d64-46b0-84ab-20e000aa90ad&
amp;data=05%7C02%7Creportingservice%40Test.com%7Cbe046aea01764287d55408dc91dbb5c7%7Cc3244fae33e14f268c4dab359072dc1e%7C0%7C0%7C638545620055778046%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&
amp;sdata=ZKQyjmggffneNGOa5xOw2pLmi1hrlFQcPJ09T%2FGA53Y%3D&reserved=0" data-linkindex="1" style="border: 0px; font: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
View Admissions/Readmissions Report results
</a>
Is it possible to have a meeting with you separately?
I have done this before with other Microsoft engineers.
I am in PST time zone, but I could work around your availability, no problem.
Thanks!!
-
King Java 500 Reputation points
2024-06-25T22:11:53.2533333+00:00 @SwathiDhanwada-MSFT I would like to follow up. Thank you.
-
SwathiDhanwada-MSFT 18,771 Reputation points
2024-06-26T10:33:23.9766667+00:00 @King Java Kindly note that do not share any PII data on the public forum ( like subscription ID, etc) . For now, I have deleted it from your comment.
If you have a support plan, you may file azure support ticket to receive immediate and dedicated assistance on this issue. If you have already created a support ticket, please share the #SR details, I'll track this internally.
-
King Java 500 Reputation points
2024-06-26T18:04:40.7366667+00:00 I am sorry about that.
I did not know that Azure subscription info is PII data.
For sure, next time, I would not post that info here.
I just created a Azure Support ticket (Case 2406260010003975), and mentioned your name.
Sign in to comment