Salesforce allows files to be attached to records. These files are stored as Base64 encoded data in the ‘Content Version’ object. SmartConnect can be used to encode these files and upload them to Salesforce. Once the attachment is imported, we will need to use the ‘Content Document Link’ object to link the document to the relevant records in Salesforce. In today’s article we go over the steps to upload a file to Salesforce and link it to a record. In my example I will be attaching a PDF to an existing Account. At the bottom of the article is a download link for the maps used in this example.

Querying Content Version

Querying the data can be tricky, but it is possible to retrieve the latest version of the attachment by filtering on the ContentDocumentLink object.
A query to retrieve all attachments on all accounts would look like:

Create Content Version

Create a map with the following information in the source. You can use a multi-data source if needed to retrieve all the necessary information.

  • Account Id in Salesforce
  • File path of the PDF file
  • Title of the document

We will select ‘Content Document Version’ as the destination object. We will map the Salesforce Id to the Description of the ‘Content Document’. This will give us a unique field to use when we link the Content Document to the Account. We will group our map on the Id field that we mapped to Description, because this is our unique identifier for the Content Document.

Content Document (Version) Mapping

The PDF file can be easily encoded with a calculated column. Here is a sample script for the calculated column.

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

Link the Content Document

We will create a second map to link the imported Content Document to the Account record. For my example I used the same data source for my child map and set it as a post map task to link all the newly imported content documents. This map will have a destination object of ‘Content Document Link’.

We will need to map the ContentDocumentID which can be populated using an entity lookup using the Description field on Content Document.  The LinkedEntityId is the Id of the Account record I want linked to this document.


Content Document Link Mapping

Updating an Attachment

There are times when we will need to update a document. Salesforce handles this by allowing for multiple versions of the same document. The only thing that will need to change in our existing map is the Title. If the Title is different, Salesforce will add a new version on the existing document.

You will not need to run the Document Link map on an update, because the existing link is still being used regardless of version. If you try to update a Document Link you will receive errors that you cannot edit the ContentDocumentId and LinkedEntityId.


Keep in mind that Salesforce is a hosted environment so it is not encouraged to store large documents this way unless absolutely necessary. The Salesforce maximum file size that can be upload via the SOAP API is 50 MB. When a document is uploaded or downloaded via the API, it is converted to base64 and stored in VersionData. This conversion increases the document size by approximately 37%. Account for the base64 conversion increase so that the file you plan to upload is less than 50 MB after conversion.

SF Attachment Maps