Overview
Querying or upserting data into Salesforce objects is a simple process with MuleSoft and the Salesforce Connector. You can retrieve a Contact record, or update an Account through a single operation generally in Anypoint Studio. But there are some use cases where you need to combine multiple operations to handle a task in Salesforce. One of which, that comes up often, is uploading and attaching a file to an object.
This article will show you how to build a flow that uploads a file using the Salesforce Connector and links it to a Contact object in a simple Mule flow. The source will be a file picked up by the File Connector but you can swap out that source with anything that results in a file in the payload.
Build Flow in Anypoint Studio
Setup Flow
We’re going to start with setting up a new project and add the necessary components into the Mule Palette. In the next section, we’ll walk through the process of configuring each component.
- Open Studio and create a new project
- In your project, add the following components into the Mule Palette
- File
- Salesforce
- Secure Properties (Optional)
- Drag and drop the following components into your flow.
- File > On New or Updated File
- Core > Transform Message
- Salesforce > Upsert
- Salesforce > Query
- Core > Transform Message
- Salesforce > Upsert
- Logger
- The flow should look like this:
Configure ‘On New or Updated File’
For the source of the flow, we’re going to set up two folders under src/main/resources. The File component will listen for and pick up the file for processing from the first folder, and then drop the file into the second folder.
- Right click on src/main/resources in the Package Explorer of your project folder and select New > Folder
- Give the folder a name (e.g. in) and click on Finish
- Repeat steps 1 and 2 but create another folder in the same directory name out. The folder structure should look like this:
- Next, select the On New or Update File component to bring up the Mule Properties window.
- Configure the following fields:
- General > Directory
- This should point to the in folder that you added to src/main/resources
- Post processing action > Move to directory
- This should point to the out folder that you added to src/main/resources
- (Optional) General > Scheduling Strategy
- You can modify this accordingly to run with a larger interval (e.g. every 30 seconds)
- General > Directory
Configure ‘Transform Message’ – Map File to ContentVersion object
The next component is a Transform Message that takes the file and maps it to the Salesforce ContentVersion object.
- Select the Transform Message component and paste the following DataWeave script into the script editor. The file is stored into the ContentVersion object as a byte array.
%dw 2.0
import * from dw::core::Binaries
output application/java
---
[{
PathOnClient: attributes.fileName,
VersionData: payload as Binary {
class: "byte[]"
}
}]
Configure Salesforce Upsert – ContentVersion
With the data mapped, the next step will be to run the Salesforce Upsert operation. This will take the file and upsert the data into the ContentVersion object.
- Select the Upsert component to open the Mule Properties window
- Click on the green 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
Configure Salesforce Query – Get 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. In order to do so, we need to run a query against the ContentDocumentId object by querying for the Id that was returned from the Upsert operation.
- Select the Salesforce Query component to open the Mule Properties window.
- The Connector configuration field should already be populated with the configuration that we set up in the previous step. If not, go back and follow steps 1 through 4.
- In the General > Salesforce query: box, paste the following SOQL script:
SELECT ContentDocumentId FROM ContentVersion WHERE Id = ':cvId'
- Under the Parameters section, click on the green plus sign and add the following.
- Name – “cvId”
- Value – payload.items[0].id
Configure ‘Transform Message’ – Map Document Link
The next component is a Transform Message that takes the ContentDocumentId from the query and a Contact object Id and maps that to a ContentDocumentLink object that will be upserted into Salesforce.
- Select the Transform Message component and paste the following DataWeave script below into the script editor.
- You’ll need to modify the script and replace the LinkedEntityId with the Id for the object that you want to link the file to. If you’re using the project from Github, you can set the Id in the properties file to be picked up.
%dw 2.0
output application/java
---
payload map ( payload01 , indexOfPayload01 ) -> {
LinkedEntityId: p('secure::sfdc.object_id'),
ContentDocumentId: payload01.ContentDocumentId
}
Configure Salesforce Upsert – Content Document Link
The mapping from the previous step will be passed to this operation in the payload to upsert and create the document link between the file and the Contact object.
- Click on the Salesforce Upsert component to open the Mule Properties window.
- Set the fields to the following values.
- Object Type: Content Document Link (ContentDocumentLink)
- External id field name: Id
Configure Logger (Optional)
This step is optional. This will just output the result from Salesforce on whether the previous operation was successful or not to the console.
- In the Message field, click on the fx button and paste the following DataWeave script
output application/json --- payload
Run Example
Now that the flow is configured, let’s go ahead and run the project to see it in action.
- Right-click on the canvas and select Run project <project name>
- Once the Console window says the project is deployed, switch to your OS file browser and navigate to your project.
- Drop a file into the in folder under src/main/resources
- After the expected interval, you should see the file moved to the out folder.
- Switch back to Anypoint Studio and review the Console. You should see the following output from Salesforce.
- Next, switch to your Salesforce instance and login.
- Navigate to the Files navigation item to see the uploaded file.
- Then switch to the Contact record that you configured the project to link the file with. If everything was configured correctly, you should see the following under Notes & Attachments
Summary
As you can see with the project you just created, uploading a file to Salesforce is incredibly easy with MuleSoft. While the project is pretty basic, you can easily extend it to your use cases to change the source of the file, the object that is linked with the file, etc… With simple drag and drop components and the ease of configuring these components to handle transformations and mappings, MuleSoft opens up endless possibilities of integrating your systems and building APIs to expose data. A working example from this post can be found here.
If you run into any problems, or have any questions, don’t hesitate to to leave a comment below.