Insert a ContentVersion into Salesforce using the REST API, particularly when implementing this functionality within a Mule application.
1. Retrieve the file content from the source.
For example, using an HTTP request:
<http:request method="GET" doc:name="File Request" doc:id="8abe537a-1048-43eb-af25-21994afc15fe" config-ref="HTTP_Request_configuration" path="/get-attachment">
<ee:repeatable-file-store-stream inMemorySize="5" bufferUnit="MB" />
</http:request>
The result payload of the HTTP request references the file content with streaming enabled.
2. Prepare the DataWeave script to handle multipart/form-data for uploading the ContentVersion.
Below is an example for inserting a ContentVersion to a ContentDocument (Files in Salesforce):
<ee:transform doc:name="Transform Message" doc:id="d557d188-4fef-4597-9d3d-212a697615bf" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output multipart/form-data boundary="boundary123"
var entityContent = {
"ContentDocumentId" : "XXXXX0695jADgGNAA1",
"ReasonForChange" : "Materials updated",
"PathOnClient" : "test.zip"
}
---
{
parts: {
entity_content: {
headers: {
"Content-Disposition": "form-data; name=\"entity_content\"",
"Content-Type": "application/json"
},
content: entityContent
},
VersionData: {
headers: {
"Content-Disposition": "form-data; name=\"VersionData\"; filename=\"" ++ entityContent.PathOnClient ++ "\"",
"Content-Type": "application/octet-stream"
},
content: payload
}
}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
3. Use the Salesforce REST API to upload the ContentVersion with the prepared DataWeave script.
<http:request method="POST" doc:name="Insert ContentVersion" doc:id="2747ebda-e702-4ff4-b193-82858b67a43d" config-ref="HTTP_Request_configuration_sf" url="https://MyDomainName.my.salesforce.com/services/data/v65.0/sobjects/ContentVersion" sendBodyMode="ALWAYS" requestStreamingMode="ALWAYS" responseTimeout="300000">
<ee:repeatable-file-store-stream inMemorySize="50" bufferUnit="MB" />
<http:headers><![CDATA[#[{
"Authorization": "Bearer " ++ vars.accessToken,
"Content-Type": "multipart/form-data; boundary=\"boundary123\""
}]]]></http:headers>
</http:request>
4. Test the application to ensure successful upload of the ContentVersion and verify the file integrity.
Attached is a sample XML file provided as a proof of concept and for testing purposes.
Disclaimer:
This article provides a suggestion that should be considered in conjunction with your specific use case and requirements and does not represent a complete solution for all circumstances.
The example is provided as a reference for your own usage and it's not part of the official Mule product so its use will be considered as a custom implementation made by the customer.
This article involves products and technologies which do not form part of the MuleSoft product set. Technical assistance for such products is limited to this article.
Reference link:
Salesforce REST API Developer Guide: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm (refer to the "Inserting a ContentVersion" section)
Compared to the Create operation in the Salesforce Connector, which relies on Salesforce SOAP API, the REST API:
Does not require Base64 encoding of the file content. This helps reduce the heap memory consumption.
Increases the file size limit from 50 MB to 2 GB.
005225390
5 KB

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.