Back

Dynamics 365 Sales Notes and Attachments

Published: Jun 06, 2018
Post Author Written by Ethan Sorenson

Dynamics 365 Sales/CRM allow notes to include attachments. SmartConnect can import and export many different file types that are used with notes. Below I’ll go into detail about the steps required to use different file types with CRM notes.

CRM notes are stored in the entity “Annotation”. You will be required to give your note a subject and a regarding value. The regarding field will be an entity lookup to the record with the note.

No attachment, just text

The simplest option is to not include an attachment. In this scenario the only fields that need to be mapped are Subject, an entity lookup value to the Regarding field, and the Description field.

Attach a text file

This will work with all common file extensions.

This option is a little more challenging than just using the note text field.

1. use a source field containing text for document

You will need to provide a Subject and Regarding Object Id same as before, but you will also need to include three other fields about the attached document.
annotation 01

The calculated column for the documentbody will convert the standard text in the text file to base64 encoding which is the way CRM expects it. The script will look something like this:

dim PlainTextBytes = System.Text.Encoding.ASCII.GetBytes(_SOURCEFIELD)
return System.Convert.ToBase64String(PlainTextBytes)

2. Reading from a text file in a folder

If you have the file path this can be done.

Using the same mapping shown above, change the calculated column to the following.

_FILEPATH can either by a source column, or a hardcoded file path including the extension.

Ex. C:UsersAdministratorDownloadsnotes.txt

return System.Convert.ToBase64String(File.ReadAllBytes(_FILEPATH))

 

Attach an Image/PDF/MP3/MP4

This will work with all common file extensions.

I tested this in two scenarios.

1. File is saved in a folder.

When the file is coming from a folder you will need to map the following five fields.
annotation 01
The calculated column mapped to document body should contain the following script.

_FILEPATH can either by a source column, or a hard-coded file path including the extension.

Ex. C:UsersAdministratorDownloadsnotes.txt

return System.Convert.ToBase64String(File.ReadAllBytes(_FILEPATH))

2. File is already in another CRM instance

You will need to map the same five fields as above, only this time you can map the file name and document body directly from the source environment, because it is already encoded correctly.

Export from CRM to file

The SmartConnect mapping window won’t be able to export to a file, but you can do it using document task.

Below is the script I used in a predocument task to take the attachement from the document body and decode it and write it to a local folder.

Dim FilePath as String = “C:UsersAdministratorDesktopeOneImage.jpeg”
Dim imageFile as FileStream = new FileStream(FilePath, FileMode.Create)
Dim Image As Array = System.Convert.FromBase64String(_DOCUMENTBODY)
imageFile.Write(Image ,0, image.Length)
imageFile.Flush()

This approach has worked for text/images/PDF/MP3 and MP4 in my testing.

annotation 02

Errors

“Attachment file size is too big”

The default size is 5 MB, but can be bumped up to 128 MB.
This Article has instructions for changing the setting. As the author of the article states, it can become very expensive if you attach multiple large files in a hosted environment.

Error messages when importing annotations are attached to the note in CRM, which can make it hard to track down if the integration failed.

If you want to take these integrations a step further you can use this article as a reference for moving a file after it has been imported.

Feeling stuck? Get the support and guidance you need to help you power through any data challenge

We're on your integration team. Connect with our people and let us know how we can help you.