Upload File And Link File To Salesforce Record

Author: Harshitha Bhat

Use case

Once after receiving a file from source, the file should be uploaded to a respective Salesforce object.

This article will show you how to upload a file and link that file to the respective salesforce object using its record Id.

Overall, the following steps will be performed:

  1. Upsert the file to Content Version object in Salesforce
  2. Retrieve ContentDocumentId from Content Version object
  3. Upsert the ContentDocumentId with respective record Id/object Id to Content Document Link object
Build Flow in Anypoint Studio
  1. Open Studio and create a new project.
    • In this case, project name is upload-file-to-salesforce
  1. In your project, drag and drop the HTTP listener.
    • In this project, file content is sent as multipart/form-data through postman, which can be seen in the following article.
  • Configure the HTTP listener configuration.
    • Click on the plus button, as shown in the below screenshot.
  • Add the respective values to run your project on the local system as shown below in the screenshot and test connection, and click OK.
    • Protocol: select HTTP
    •  Host: select All Interfaces [] (default)
    •  Port: 8081
  • Specify the path as an /upload for the HTTP listener.
  1. Extracting the file content from payload (of type multipart/form-data received from postman) and storing it under variable initialPayload.
  1. Add Transform Message and rename it as Transform payload to Content Version object type
    • Create a variable called payloadForContentVersion
  • Add the below dataweave script to this payloadForContentVersion variable.
  • Add variable called object_id into transform message, which contains the record Id of the salesforce object.
  1. Configure Salesforce Upsert – ContentVersion
    • The next step is to perform the Salesforce Upsert operation. This will take the file and upsert the data into the ContentVersion object.
      1. Drag and drop Upsert component to the flow
  1. Click on the plus sign next to the Connector configuration dropdown field in the Basic Settings section.
  2. Fill in the following fields under the General tab for a Basic Authentication connection:
    • Username
    • Password
    • Security Token
  3. Click on OK
  1. In the Mule Properties window, set the fields to the following values.
    • Object Type: ContentVersion
    • External id field name: Id
    • Records: vars.payloadForContentVersion (which contains the payload prepared for contentVersion object)
  1. Storing the payload returned by upserting payload to contentVersion under variable called contentVersion
  1. Add Salesforce Query component and rename it as Retrieve Content Document Id
  • After the file has been uploaded to the ContentVersion object, we need to get the Content Document Id that gets generated by Salesforce.
  • To retrieve that Content Document Id, query ContentDocumentId object.
    1. The Connector configuration field will populate with the configuration that we set up in the previous step. 
    2. Under salesforce query tab, paste the following SOQL script:
      • SELECT ContentDocumentId FROM ContentVersion WHERE Id = ‘:Id’
    3. Under the Parameters tab, click on the plus sign and add the following.
      • Name – “Id”
      • Value – vars.contentVersion.items[0].id
  • Store the payload returned from Retrieve Content Document Id (salesforce query component) to variable called contentDocument
  1. Add ‘Transform Message’ and rename it to Transform payload to Content Document Link object type.
    • It takes the ContentDocumentId from the query and a record Id (from the object_id variable) of the object and maps that to a ContentDocumentLink object that will be upserted into Salesforce.
  1. Add Salesforce Upsert and rename it to Create Content Document Link
    • Drag and drop Upsert component to the flow
  • The Connector configuration field will populate with the configuration that we set up in the previous step.
  • In the Mule Properties window, set the fields to the following values.
    1. Object Type: Content Document Link (ContentDocumentLink)
    2. External id field name: Id
    3. Records: vars.payloadForContentDocumentLink (which contains the payload prepared for contentDocumentLink object)
  • Storing the payload returned by upserting payload to contentDocumentLink under variable called contentDocumentLink
  • The payload returned by this upsert operation is as follows.
  1. Add Set Payload and rename it to Set response payload (Optional)
  • To represent the payload with necessary fields, response payload is mapped with created, success and id fields.
Deploy and run
  1. Right-click on the canvas and select Run project <project name>
  2. Once the project is deployed, switch to your postman
    1. Select method as POST
    2. Set URL with http://localhost:8081/upload 
  1. Click on body and select form-data
    1. Specify file under key by selecting File under the dropdown list
  1. Click on Select Files and select the file from file explorer.
  1. Click on Send
  1. Next, switch to your Salesforce instance and login
  2. Navigate to the Files navigation item to see the uploaded file

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.