Loading
Salesforce now sends email only from verified domains. Read More

How To Insert a ContentVersion to Salesforce Using REST API

Publish Date: Oct 15, 2025
Task

Insert a ContentVersion into Salesforce using the REST API, particularly when implementing this functionality within a Mule application.

Steps

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.

  • Login to Salesforce.
  • Select "Files" from App Launcher.
  • Click the dropdown of the file and select "View File Details".
  • Click "View All" in the "Versions" widget.
  • Click the version number to download the file and verify its 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.

Additional Resources

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.

Knowledge Article Number

005225390

Attachments

insertContentVersion.xml

5 KB

 
Loading
Salesforce Help | Article