Upload and link files using the Salesforce Connector in Mule 4

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.

  1. Open Studio and create a new project
  1. In your project, add the following components into the Mule Palette
    • File
    • Salesforce
    • Secure Properties (Optional)
  1. 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
  2. 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.

  1. Right click on src/main/resources in the Package Explorer of your project folder and select New > Folder
  1. Give the folder a name (e.g. in) and click on Finish
  1. Repeat steps 1 and 2 but create another folder in the same directory name out. The folder structure should look like this:
  1. Next, select the On New or Update File component to bring up the Mule Properties window. 
  2. Configure the following fields:
    1. General > Directory
      1. This should point to the in folder that you added to src/main/resources
    2. Post processing action > Move to directory
      1. This should point to the out folder that you added to src/main/resources
    3. (Optional) General > Scheduling Strategy
      1. You can modify this accordingly to run with a larger interval (e.g. every 30 seconds)

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.

  1. 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.

  1. Select the Upsert component to open the Mule Properties window
  2. Click on the green plus sign next to the Connector configuration dropdown field in the Basic Settings section.
  1. Fill in the following fields under the General tab for a Basic Authentication connection:
    • Username
    • Password
    • Security Token
  1. Click on OK
  2. 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.

  1. Select the Salesforce Query component to open the Mule Properties window.
  2. 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.
  3. In the General > Salesforce query: box, paste the following SOQL script:
SELECT ContentDocumentId FROM ContentVersion WHERE Id = ':cvId'
  1. 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.

  1. Select the Transform Message component and paste the following DataWeave script below into the script editor.
  2. 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.

  1. Click on the Salesforce Upsert component to open the Mule Properties window.
  2. 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.

  1. 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.

  1. Right-click on the canvas and select Run project <project name>
  1. Once the Console window says the project is deployed, switch to your OS file browser and navigate to your project. 
  2. Drop a file into the in folder under src/main/resources
  3. After the expected interval, you should see the file moved to the out folder.
  4. Switch back to Anypoint Studio and review the Console. You should see the following output from Salesforce.
  1. Next, switch to your Salesforce instance and login.
  2. Navigate to the Files navigation item to see the uploaded file.
  1. 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.


Posted

in

,

by

Comments