This question comes up occasionally in my conversations with customers. Often there is a need to programmatically upload an attachment (a pdf or an image) file to Salesforce using MuleSoft. If the integration developer is not very familiar with the Salesforce Platform, it usually confusing due to the way Salesforce manages the attachments.
As shown in the following ER diagram, there are multiple entities involved in storing and managing the attachments in Salesforce
Things to note:
Salesforce attachments are stored in ContentDocument (and ContentVersion) objects.
The ContentDocument object is used to retrieve, query, update and delete the latest version of a document.
The ContentVersion object is used to create, query, retrieve, search, edit, and update a specific version of a Salesforce CRM Content document.
The attachment is stored in ContentDocument (Or ContentVersion) and linked to any sObject (Contact, Account etc) via ContentDocumentLink Object
Thus, to successfully upload an attachment to Salesforce following needs to happen
Upsert an attachment (file) to ContentDocument/ContentVersion object
Query the ContentDocument/ContentVersion to get the ContentDocumentId
Use the ContentDocumentId to link to your sObject (Account, Lead Opportunity, etc) by upserting the relationship in the ContentDocumentLink object
Following flow chart shows the general approach to upload an attachment to Salesforce: