cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mlcarter815
Frequent Visitor

base64 Decode PDF from Note Entity for Email Attachment

I know from multiple closed questions on this board that in order to attach a PDF that is stored in the CDS Note entity to an email it needs to be base64 decoded. I've tried passing the document body into the base64tobinary function for the Attachment Content in 'Send an email (V2)'. That throws an error because the document body is a JSON object and it's the ContentBytes inside that object that need decoded. However, I also can't parse the JSON because of the '/' that are throughout it. 

 

Any help on how I'm supposed to decode this and add it as an Attachment Content is appreciated. 

 

Edit to add screenshots below:

 

First, I get a list of CDS Note entities that are tied to the Purchase Order custom entity record that I already have through the trigger body. I expect there to only be one Note entity, which is why I have the Top Count = 1. 

Get Notes.png

 

 

Then I have the necessary loops for the Project Terms custom entity and the Notes entity to populate the email. 

CreateEmail.png

 

If I click in the "Attachments Content -" box, I get the following options for the dynamic content. Note that the Document Body does not appear in the list of dynamic content. 

Dynamic content list.png

 

This is the JSON that I get back from the step pulling the Note entity. Notice that in the red box that there are '/' in the JSON which prevents the JSON from being parsed if I wanted to. I have tried using 

base64ToString(items('Apply_to_each_2')?['documentbody']) to decode the ContentBytes like I have seen in past issues on this board. That fails with the error 'Unable to process template language expressions in action 'Send_an_email_(V2)' inputs at line '1' and column '2691': 'The template language function 'base64ToString' was invoked with a parameter that is not valid. The value cannot be decoded from base64 representation.'.' 

Notes Object.png

 

Edit to my edit: It appears that I may be adding the attachment incorrectly to the CDS Note entity in a different Flow. For some reason the 'Attachments Content' does not appear in the Dynamic Content menu when I click in the 'Document' field for adding a new Note entity to CDS. 

1 ACCEPTED SOLUTION

Accepted Solutions

I figured it out. There was an issue with how I was storing the document. I ended up going a different route. 

View solution in original post

11 REPLIES 11
abm
Super User III
Super User III

Hi @mlcarter815 

 

I could help you via screenshare?  Send me the details via email.

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blogPower Automate Video Tutorials
abm
Super User III
Super User III

Hi @mlcarter815 

 

I am working on this.

 

 

 



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blogPower Automate Video Tutorials
abm
Super User III
Super User III

Hi @mlcarter815 

 

Standard entities and Custom entity attachments are stored under Notes.

 

Email attachments are stored under Attachments (ActivityMimeAttachment) entity. Here are the steps.

 

image.png

 

Above I have hardcoded the attachment id to get my single attachment against my email.

 

Documentation says we can filter by objectid but I couldn't find the email id (Objectid) under the attachment run history  😞

 

Next we need to get the Filename and content

 

image.png

 

 

Here is my result

 

image.png

 

 

Couple of things I am still looking for is:

 

1. Why Attachment entity doesn't return the objectid which the attachment is related to? Here in our case email id?

https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/entities/activ...

 

Below is the attachment run history and no objectid (Our case email id) is returned.

 

[
{
"@odata.type": "#Microsoft.Dynamics.CRM.attachment",
"@odata.id": "https://xxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)",
"@odata.etag": "W/\"14863436\"",
"@odata.editLink": "attachments(85f9af51-ba6b-ea11-a811-0022480030d2)",
"mimetype": "application/pdf",
"filename": "InvoiceLaptop.pdf",
"attachmentid@odata.type": "#Guid",
"attachmentid": "85f9af51-ba6b-ea11-a811-0022480030d2",
"filesize": 136928,
"versionnumber@odata.type": "#Int64",
"versionnumber": 14863436,
"body": "JVBERi0xLjQKJeLjz9MKMyAwIG9iaiA8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvQWx0ZXJuYXRlL0RldmljZVJHQi9MZW5ndGggMjU5Mi9OIDM+PnN0cmVhbQp4nJ2Wd1RT2RaHz703vdASIiAl9F4VCCDSO0iRLgohCRAgAUJCsyuiAiOKiDRFkEEBBxwdiowVUSwMir1PkEFEGQdHERXLu9G1Rte8ee/Nm98fZ31rn7P3PbuctS4AFN90jiAbVgBAIBSLwnzcmdExsUz8EIABEeCANQBsTnamW0hIIEDl5+XBzEYPgS",
"Attachment_SyncErrors@odata.associationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/At...",
"Attachment_SyncErrors@odata.navigationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/At...",
"attachment_activity_mime_attachments@odata.associationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/at...",
"attachment_activity_mime_attachments@odata.navigationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/at...",
"userentityinstancedata_attachment@odata.associationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/us...",
"userentityinstancedata_attachment@odata.navigationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/us..."
}
]

 

2. If we have more than one attachment under the same email how can we attach all the documents in one email? I have tried couple of things like declared an object and append the filename and content

{
"Name": "",
"ContentBytes":
}

 

Also tried

 

{
"mime/type": "",
"ContentBytes":
}

 

But no luck at all.

 

I am tagging @LeeHarris  for some help here. Lee has answered the below post regarding attachments

 

https://powerusers.microsoft.com/t5/Building-Flows/Dynamics-365-Use-flow-to-take-an-attachment-from-...

 

@LeeHarris  any help is appreciated.

 

Thanks

 

 

 



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blogPower Automate Video Tutorials

Hi @abm 

Just to clarify my understanding of the issue here. @mlcarter815  you are trying to get an attachment from a Note record in CDS and send it in a email?

The content of CDS Note attachments are found in the "documentbody" property of the "annotations" entity. If you know the Note id then you can query annotations in order to get the Note record and the documentbody value. Notes with attachments will have the "isdocument" property set to true so you can use that as well if you want to check that the Note has an attachment before processing it. I don't think that you will need to convert the documentbody value if then attaching to an email, simply use the value, as is, in the Attachment content.

If you are trying to get details on an attachment for an email record then you need to be querying the "activitymimeattachments" entity. You will then have an objectid property which provides the id of the record that the attachment relates to. In addition, this record will also have an "objecttypecode" which indicates the type of record that it relates to. Useful for filters if you only want attachments for a particular activity type returned. The attachmentid in this activitymimeattachments record shows the link to the actual attachment record but the content of "body" in both should be identical, so you only need the activitymimeattachment record.

Hope that helps.

 


Did I answer your question? Mark my post as a solution!

Proud to be a Flownaut!


Follow me on Twitter at @QG_LeeJHarris

Or on LinkedIn at in/leejharris

abm
Super User III
Super User III

Hi @LeeHarris 

 

Thanks for your reply. 

 

For the notes entity I can get all the details as expected. That's no problem at all.

 

Yes for the email attachments it is stored under activitymimeattachment entity.

 

But for the activity activity_mime_attachments I can't see the object id. See below JSON returned from Get All Attachments with no filter.

 

[
{
"@odata.type": "#Microsoft.Dynamics.CRM.attachment",
"@odata.id": "https://xxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)",
"@odata.etag": "W/\"14863436\"",
"@odata.editLink": "attachments(85f9af51-ba6b-ea11-a811-0022480030d2)",
"mimetype": "application/pdf",
"filename": "InvoiceLaptop.pdf",
"attachmentid@odata.type": "#Guid",
"attachmentid": "85f9af51-ba6b-ea11-a811-0022480030d2",
"filesize": 136928,
"versionnumber@odata.type": "#Int64",
"versionnumber": 14863436,
"body": "JVBERi0xLjQKJeLjz9MKMyAwIG9iaiA8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvQWx0ZXJuYXRlL0RldmljZVJHQi9MZW5ndGggMjU5Mi9OIDM+PnN0cmVhbQp4nJ2Wd1RT2RaHz703vdASIiAl9F4VCCDSO0iRLgohCRAgAUJCsyuiAiOKiDRFkEEBBxwdiowVUSwMir1PkEFEGQdHERXLu9G1Rte8ee/Nm98fZ31rn7P3PbuctS4AFN90jiAbVgBAIBSLwnzcmdExsUz8EIABEeCANQBsTnamW0hIIEDl5+XBzEYPgS",
"Attachment_SyncErrors@odata.associationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/At...",
"Attachment_SyncErrors@odata.navigationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/At...",
"attachment_activity_mime_attachments@odata.associationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/at...",
"attachment_activity_mime_attachments@odata.navigationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/at...",
"userentityinstancedata_attachment@odata.associationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/us...",
"userentityinstancedata_attachment@odata.navigationLink": "https://xxxxxx.crm11.dynamics.com/api/data/v9.0/attachments(85f9af51-ba6b-ea11-a811-0022480030d2)/us..."
}
]

 

Thanks for your help.

 



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blogPower Automate Video Tutorials

Hi @abm 

If you run the below query in your browser (replacing the xxxxx with your instance name) you should get the activitymimeattachment record back for the attachment that you are looking at. The body property for this record should match the body value in the attachment record so there is no need to query attachments directly. You should also then have access to the objectid. This activitymimeattachment record is effectively working as the join between both the activity (which could be one of many entity types including email) and the attachment record.

 

https://xxxxx.crm11.dynamics.com/api/data/v9.0/activitymimeattachments?$filter=_attachmentid_value eq  85f9af51-ba6b-ea11-a811-0022480030d2

 


Did I answer your question? Mark my post as a solution!

Proud to be a Flownaut!


Follow me on Twitter at @QG_LeeJHarris

Or on LinkedIn at in/leejharris

abm
Super User III
Super User III

Hi @LeeHarris 

 

Thanks for your reply 🙂

 

May I ask you you try the CDS connector and get all attachments. Why the object id is missing from JSON? Yes the documentation says it and via SDK I can access it using custom code (C#).

 

Is this a limitation in flow CDS connector?

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blogPower Automate Video Tutorials

Hi @abm 

I have just tried this with the Common Data Service connector and I am seeing 2 options for Attachments in the drop down.

 

image.png

One of these is the attachment entity and the other is the activitymimeattachment entity. You can see from the below responses that only the activitymimeattachment returns the objectid.

 

Attachment

{
  "@odata.context": "https://uk-001.azure-apim.net/apim/commondataservice/ba6154td4b454efc88f3f003e3535326/$metadata#datasets('org654s54af.crm4')/tables('attachments')/items",
  "value": [
    {
      "@odata.id": "https:/instancename.crm4.dynamics.com/api/data/v9.0/attachments(14cfc44a-70ea-e911-a811-000d3ab4d306)",
      "@odata.etag": "",
      "ItemInternalId": "14cfc44a-70ea-e911-a811-000d3ab4d306",
      "versionnumber": 226802206,
      "filename": "image001.jpg",
      "mimetype": "image/jpeg",
      "body": "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIfIiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/wAALCAAwADABAREA/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEBAAA/APZqKKKKKKKKKKKKKKKKKKKKKKKKKKKKK//9k=",
      "attachmentid": "14cfc44a-70ea-e911-a811-000d3ab4d306",
      "filesize": 359,
      "prefix": null,
      "subject": null
    }
  ]
}

 

ActivityMimeAttachment

{
  "@odata.context": "https://uk-001.azure-apim.net/apim/commondataservice/na6154cb4b454jfc88f3f003e3535326/$metadata#datasets('org111i53af.crm4')/tables('activitymimeattachments')/items",
  "value": [
    {
      "@odata.id": "https://instancename.crm4.dynamics.com/api/data/v9.0/activitymimeattachments(2b516b8d-2785-4e25-953d-000067ec8e07)",
      "@odata.etag": "",
      "ItemInternalId": "2b516b8d-2785-4e25-953d-000067ec8e07",
      "overwritetime": "1900-01-01",
      "attachmentnumber": 1,
      "solutionid": "fd140aae-4df4-11dd-bd17-0019b9312238",
      "_objectid_value": "2eb54855-28a8-e711-ad38-005056c00008",
      "_objectid_type": "emails",
      "activitymimeattachmentidunique": "57b6fa9a-6b6c-4d21-aaa4-185521d7b641",
      "isfollowed": false,
      "activitymimeattachmentid": "2b516b8d-2785-4e25-953d-000067ec8e07",
      "_attachmentid_value": "f71d8710-55ca-e711-8144-e0071b662001",
      "_attachmentid_type": "attachments",
      "ismanaged": false,
      "objecttypecode": "email",
      "componentstate": 0,
      "_componentstate_label": "Published",
      "versionnumber": 25220775,
      "subject": null,
      "attachmentcontentid": null,
      "anonymouslink": null
    }
  ]
}

 

As they both appear the same in the entity selection drop down list, the easiest way to guarantee you are fetching the correct data is to select the custom option (very bottom of the drop down) and type the value "activitymimeattachments".

 


Did I answer your question? Mark my post as a solution!

Proud to be a Flownaut!


Follow me on Twitter at @QG_LeeJHarris

Or on LinkedIn at in/leejharris

abm
Super User III
Super User III

Hi @LeeHarris 

 

Thanks for the clarification. I got mixed up with two Attachments there. Thanks for clarifying this and taking time to show me in a screenshot.

 

Cheers



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blogPower Automate Video Tutorials
mlcarter815
Frequent Visitor

I'll be updating the OP with screenshots of what I'm doing after our planning meeting this morning.

I figured it out. There was an issue with how I was storing the document. I ended up going a different route. 

View solution in original post

Helpful resources

Announcements
Process Advisor

Introducing Process Advisor

Check out the new Process Advisor community forum board!

MPA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Top Solution Authors
Top Kudoed Authors
Users online (35,292)