Upload and link file to Salesforce record

  • March 10, 2022

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

— By Harshitha Bhat