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:
- Upsert the file to Content Version object in Salesforce.
- Retrieve ContentDocumentId from Content Version object.
- Upsert the ContentDocumentId with respective record Id/object Id to Content Document Link object.
Build Flow in Anypoint Studio
- Open Studio and create a new project.
- a. In this case, project name is upload-file-to-salesforce
- 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.
- 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
- Specify the path as an /upload for the HTTP listener.
- Extracting the file content from payload (of type multipart/form-data received from postman) and storing it under variable initialPayload.
- 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.
- 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.
- Drag and drop Upsert component to the flow.
- Click on the plus sign next to the Connector configuration dropdown field in the Basic Settings section.
- Fill in the following fields under the General tab for a Basic Authentication connection:
- Username
- Password
- Security Token
- Click on OK.
- 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)
- Storing the payload returned by upserting payload to contentVersion under variable called contentVersion
- 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.
- The Connector configuration field will populate with the configuration that we set up in the previous step.
- Under salesforce query tab, paste the following SOQL script:
- SELECT ContentDocumentId FROM ContentVersion WHERE Id = ‘:Id’
- 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
- 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.
- 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.
- Object Type: Content Document Link (ContentDocumentLink)
- External id field name: Id
- 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.
- 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
- Right-click on the canvas and select Run project <project name>
-
Once the project is deployed, switch to your postman.
- Select method as POST
- Set URL with http://localhost:8081/upload
-
Click on body and select form-data.
- Specify file under key by selecting File under the dropdown list.
- Click on Select Files and select the file from file explorer.
- Click on Send.
- Next, switch to your Salesforce instance and login.
- Navigate to the Files navigation item to see the uploaded file.
— By Harshitha Bhat