Contact Us 1-800-596-4880

Salesforce Composite Connector 2.6 - Mule 4

Anypoint Connector for Salesforce Composite enables you to work with the Salesforce Batch and SObject Tree APIs. The connector exposes convenient methods for preparing subordinate requests, executing them in one batch, and parsing the results. Also it offers the posibility of creating SObject Trees through an operation separate from the batch ones, called createSobjectTree. This connector provides DataSense for preparing subordinate requests and parsing responses. DataSense is also provided for execution, and for creating SObject trees.

For information about compatibility and fixed issues, see Salesforce Composite Connector Release Notes

Before You Begin

To use this connector, you should be familiar with:

  • Salesforce Composite

  • Mule runtime engine (Mule) concepts, including elements in a Mule flow and global elements

  • Anypoint Connectors

  • Anypoint Studio

You must have login credentials to test your connection to the target resource.

POM File Information

<dependency>
  <groupId>com.mulesoft.connectors</groupId>
  <artifactId>mule-salesforce-composite-connector</artifactId>
  <version>x.x.x</version>
  <classifier>mule-plugin</classifier>
</dependency>

Verify that the connector version corresponds to the one you are using.

To obtain the most up-to-date pom.xml file information:

  1. Go to Anypoint Exchange.

  2. In Exchange, click Login and supply your Anypoint Platform username and password.

  3. In Exchange, search for the connector name.

  4. Click Dependency Snippets.

Add the Connector in Studio

  1. In Studio, create a Mule project.

  2. In the Mule Palette view, click (X) Search in Exchange.

  3. In Add Modules to Project, type composite in the search field.

  4. Click this connector’s name in Available modules.

  5. Click Add.

  6. Click Finish.

When Studio has an update, a message displays in the lower right corner, which you can click to install the update.

Configure the Connector in Studio

  1. Drag the connector to the Studio canvas.

  2. To create a global element for the connector, configure these fields:

    • OAuth with Username and Password Authentication:

      • Consumer Key
        The consumer key for the Salesforce connected app

      • Consumer Secret
        The consumer secret for the connector to access Salesforce

      • Username
        The Salesforce username

      • Password
        The corresponding password

      • Security Token
        The corresponding security token

        Studio basic auth

Studio Example

  • Listener(HTTP)
    Accepts data from HTTP requests

  • Transform message
    Transforms the HTTP input accordingly

  • Salesforce Composite Connector
    Connects with Salesforce and executes the create SObject tree operation

  • Transform message
    Outputs the results of the Create SObject tree operation in JSON format

    Studio use case

XML for the Studio Example

Paste this XML code into Anypoint Studio to experiment with the flow described in the previous section.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:salesforce-composite="http://www.mulesoft.org/schema/mule/salesforce-composite"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core"
   xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
   http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/salesforce-composite
http://www.mulesoft.org/schema/mule/salesforce-composite/current/mule-salesforce-composite.xsd">
	<configuration-properties file="mule-app.properties"/>
	<http:listener-config name="HTTP_Listener_config"
   doc:name="HTTP Listener config">
		<http:listener-connection host="localhost" port="8081" />
	</http:listener-config>
	<salesforce-composite:composite-config name="Salesforce_Composite_Salesforce_Composite"
   doc:name="Salesforce Composite Salesforce Composite">
		<salesforce-composite:oauth-user-pass-connection
      consumerKey="${consumerKey}"
      consumerSecret="${consumerSecret}"
      username="${username}"
      password="${password}"
      securityToken="${securityToken}"
      tokenEndpoint="${tokenEndpoint}" />
	</salesforce-composite:composite-config>
	<flow name="salesforce-composite-create-sobject-trees">
		<http:listener doc:name="Listener"
      path="/createSObjectTree"
      config-ref="HTTP_Listener_config"/>
		<ee:transform doc:name="Transform HTTP input for Create SObject Tree">
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/java
---
payload.records map ((record , indexOfRecord) -> {
	"attributes": {
		"type": record.attributes.objectType,
		"referenceId": record.attributes.referenceId
	},
	"Name": record.Name,
	"Phone": record.Phone,
	"Website": record.Website,
	"NumberOfEmployees": record.NumberOfEmployees as Number,
	("ChildAccounts": {
		"records": record.ChildAccounts.records map ((record01, indexOfRecord01) -> {
			"attributes": {
				"type": record01.attributes.objectType,
				"referenceId": record01.attributes.referenceId
			},
			"Name": record01.Name,
			"Phone": record01.Phone,
			"Website": record01.Website,
			"NumberOfEmployees": record01.NumberOfEmployees as Number
		})
	}),
	"Contacts": {
		"records": record.Contacts.records map ((record01, indexOfRecord01) -> {
			"attributes": {
				"type": record01.attributes.objectType,
				"referenceId": record01.attributes.referenceId
			},
			"LastName": record01.LastName,
			"Email": record01.Email,
			"Title": record01.Title
		})
	}
})
]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<salesforce-composite:create-sobject-tree
      doc:name="Create SObject tree"
      config-ref="Salesforce_Composite_Salesforce_Composite"
      type="Account"/>
		<ee:transform doc:name="Transform Message">
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
	</flow>
</mule>

HTTP Input for the Studio Example

An example of HTTP input for the previous Studio example is:

{
   "records":[
      {
         "ChildAccounts":{
            "records":[
               {
                  "Phone":"4321098765",
                  "Website":"www.salesforce.com",
                  "attributes":{
                     "objectType":"Account",
                     "referenceId":"ref5"
                  },
                  "NumberOfEmployees":"10",
                  "Name":"ChildAccount1"
               }
            ]
         },
         "Contacts":{
            "records":[
               {
                  "Email":"sample@salesforce.com",
                  "Title":"President",
                  "attributes":{
                     "objectType":"Contact",
                     "referenceId":"ref6"
                  },
                  "LastName":"Jones"
               }
            ]
         },
         "Phone":"9876543210",
         "Website":"www.salesforce.com",
         "attributes":{
            "objectType":"Account",
            "referenceId":"ref4"
         },
         "NumberOfEmployees":"101",
         "Name":"CreateSobjectTreeAccount2"
      },
      {
         "Contacts":{
            "records":[
               {
                  "Email":"sample@salesforce.com",
                  "Title":"President",
                  "attributes":{
                     "objectType":"Contact",
                     "referenceId":"ref2"
                  },
                  "LastName":"Smith"
               },
               {
                  "Email":"sample@salesforce.com",
                  "Title":"Vice President",
                  "attributes":{
                     "objectType":"Contact",
                     "referenceId":"ref3"
                  },
                  "LastName":"Evans"
               }
            ]
         },
         "Phone":"1234567890",
         "Website":"www.salesforce.com",
         "attributes":{
            "objectType":"Account",
            "referenceId":"ref1"
         },
         "NumberOfEmployees":"100",
         "Name":"CreateSobjectTreeAccount1"
      }
   ]
}

Pre-Query Example

This example provides metadata for an object to be queried, generates two subrequests that contain this metadata, and then executes the subrequests. The following screenshots show the flow for the Pre-Query example:

Pre-Query flow in Studio
Figure 1. The flow for this example includes an HTTP Listener component, Transform components, and multiple Salesfore Composite operations.

Start Creating the Flow by Adding HTTP Listener

HTTP Listener initiates the flow when it detects an event in the executeFlow path:

  1. Create a new Mule project in Studio.

  2. In the Mule Palette view, search for HTTP and select the Listener operation.

  3. Drag the Listener operation onto the canvas.

  4. In the Listener properties window, set the Path field to /executeFlow.

Add the First Transform Message Component

This Transform Message component sets a name for the object to be created and passes the name to the Pre create operation:

  1. In the Mule Palette view, search for Transform Message.

  2. Drag the Transform Message component onto the canvas, to the right of Listener.

  3. Enter the name MyNewAccount:

    %dw 2.0
    %output application/json
    ---
    {
       Name: "MyNewAccount"
    }

Add the Pre Create Operation

The Pre create operation provides metatdata for creating an object called NewAccount. It generates a subrequest for the executeCompositeBatch operation:

  1. In the Mule Palette view, search for Salesforce and select the Salesforce Composite Pre create operation.

  2. Drag the Pre create operation to the right of Transform Message.

  3. Click the green plus icon (+) to the right of the Connector configuration field to access the Salesforce Composite global element configuration fields.

  4. Complete the fields using variables, as follows:

    Salesforce Composite Connector global element configuration
    Figure 2. Complete fields in the General section to create a reusable global element for Salesforce Composite Connector.
  5. In the Pre create properties window, enter Account in the Type field.

  6. In the properties file src/main/mule/mule-app.properties, declare and enter values for the variables in the global element.

Add the Second Transform Message Component

This Transform Message component sets values for the ID and Type fields, and then passes these values to the Pre query operation:

  1. Drag a second Transform Message component to the right of Pre create.

  2. Enter the following name-value pairs:

    %dw 2.0
    %output application/json
    ---
    {
    "Id": payload.Id,
    "Fields" : ["Id", "Name"],
    "Type": "Account"
    }

Add the Pre Query Operation

The Pre query operation provides metadata for an object to be queried. It generates a subrequest for the Execute composite batch operation.

  1. Drag a Salesforce Composite Pre query operation to the right of the second Transform Message.

  2. In the Query field, enter the following query:

    Select Name from Account WHERE Name LIKE '%:name %'

Add the Third Transform Message Component

This Transform Message operation passes the output of the Pre query operation to the Execute composite batch operation:

  1. Drag a third Transform Message component to the right of Pre query.

  2. Set the output to payload:

    %dw 2.0
    %output application/java
    ---
    [
    payload
    ]

Add the Execute Composite Batch Operation

The Execute composite batch operation executes the subrequests that the Pre create and Pre query operations created:

Drag a Salesforce Composite Execute composite batch operation to the right of the third Transform Message.

Add the Fourth Transform Message Component

This Transform Message operation converts the output to JSON format:

  1. Drag a fourth Transform Message component to the right of Execute composite batch.

  2. Set the output to application/json:

    %dw 2.0
    %output application/json
    ---
    payload

XML for the Pre-Query Example

Paste this code into your XML editor to quickly load the flow for this example to your Mule app. If needed, change the values to reflect your environment.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:salesforce-composite="http://www.mulesoft.org/schema/mule/salesforce-composite" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
   xmlns:http="http://www.mulesoft.org/schema/mule/http"
   xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/salesforce-composite http://www.mulesoft.org/schema/mule/salesforce-composite/current/mule-salesforce-composite.xsd">
   <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
      <http:listener-connection host="0.0.0.0" port="8081" />
   </http:listener-config>
   <salesforce-composite:composite-config name="Salesforce_Composite_Config" doc:name="Salesforce Composite Config" >
      <salesforce-composite:oauth-user-pass-connection consumerKey="${consumerKey}" consumerSecret="${consumerSecret}" username="${username}" password="${password}" securityToken="${securityToken}" />
   </salesforce-composite:composite-config>
   <flow name="composite-prequery-exampleFlow" >
      <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/executeFlow"/>
      <ee:transform doc:name="Transform Message" >
         <ee:message >
            <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
{
   Name: "MyNewAccount"
}]]></ee:set-payload>
         </ee:message>
      </ee:transform>
      <salesforce-composite:pre-create type="Account" doc:name="Pre create" config-ref="Salesforce_Composite_Config">
      </salesforce-composite:pre-create>
      <ee:transform doc:name="Transform Message" >
         <ee:message >
            <ee:set-payload ><![CDATA[output application/java
---
{
   "Id": payload.Id,
   "Fields" : ["Id", "Name"],
   "Type": "Account"
}]]></ee:set-payload>
         </ee:message>
      </ee:transform>
      <salesforce-composite:pre-query doc:name="Pre query" config-ref="Salesforce_Composite_Config">
         <salesforce-composite:query >Select Name from Account WHERE Name LIKE '%:name %'
                    </salesforce-composite:query>
         <salesforce-composite:parameters ><![CDATA[#[output applicaton/java
---
{
   "name" : "MyNewAccount"
}]]]></salesforce-composite:parameters>
      </salesforce-composite:pre-query>
      <ee:transform doc:name="Transform Message" >
         <ee:message >
            <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
[
   payload
]]]></ee:set-payload>
         </ee:message>
      </ee:transform>
      <salesforce-composite:execute-composite-batch doc:name="Execute composite batch" config-ref="Salesforce_Composite_Config"/>
      <ee:transform doc:name="Transform Message" >
         <ee:message >
            <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
         </ee:message>
      </ee:transform>
   </flow>
</mule>
View on GitHub