Hello #Muleys #Trailblazers,
In our previous blog post, we have seen how to configure MuleSoft Composer in MuleSoft AnyPoint Studio. If you missed this Here is the Link for you.
Working with SObjectTree
- HTTP ListenerAccepts data from HTTP requests
- Transform MessageTransforms the HTTP input
- Create sobject tree Connects with Salesforce and creates an SObject tree
Step1 – Create a Mule Project
In Studio, create a new Mule project in which to add and configure the connector:
- Select File > New > Mule Project.
- Enter a name for your Mule project and click Finish.
Step2 – Configure a Source
A source initiates a flow when a specified condition is met. You can configure one of these sources to use with Salesforce Composite Connector: For the demo, we are going to Use HTTP Listener
- HTTP Listener – Initiates a flow each time it receives a request on the configured host and port
For example, to configure an HTTP listener, follow these steps:
- In the Mule Palette view, select HTTP > Listener.
- Drag Listener to the Studio canvas.
- On the Listener configuration screen, optionally change the value of the Display Name field.
- Specify a value for the Path field.
- Click the plus sign (+) next to Connector configuration to configure a global element that can be used by all instances of HTTP Listener in the app.
- On the General tab, specify connection information for the connector.
- Click Test Connection to confirm that Mule can connect with the specified server.
- Click OK.
- Refer below images
Step3 – Add a Composite Connector Operation to the Flow
To add an operation for Salesforce Composite Connector, follow these steps:
- In the Mule Palette view, select Salesforce Composite and then select the create sobject tree operation.
- Drag the operation onto the Studio canvas to the right of the input source.
- Click the plus sign (+) next to Connector configuration to configure a global element that can be used by all instances of Salesforce Composite in the app.
- Select “OAuth JWT” for connection. To configure visit “How to Authenticate Salesforce using JWT from Mulesoft”
- On the General tab, specify connection information for the connector.
- Click Test Connection to confirm that Mule can connect with the specified server.
- In the Create sobject tree properties window, enter
Account
in the SObject Root Type field - Click OK.
In the demo, we are going to create an Account record and Multiple Contact Records related to that Account & An Opportunity Record related to that Account as well using a Single Request. Here is what MuleSoft Composite Connector can do.
- Used for creating one or more SObject trees having root records of the specified type.
- SObject Tree is a collection of nested, parent-child records with a single root record.
- In the request, we should provide the type and reference ID of each record
Here is the Request that we are going to use for the demo
Request Body
The request body contains a records collection that includes sObject trees. Each record will have following properties
- Attributes: Indicate attribute of a particular record Has two sub-properties
- objectType: record’s type (example: Account)
- referenceId: Reference ID for this record. Must be unique in the context of the request and start with an alphanumeric character.
- Object fields: Field and value for this record
- Child Relationships: Indicate child relationship of this record, for example
- Contacts
- Opportunities
- Cases
- For Any Custom Child Object Use RelationShip Name Like Invoices__r
The request can contain the following:
- Up to a total of 200 records across all trees
- Up to five records of different types
- SObject trees up to five levels deep
Transform Message
The Transform Message component uses input data to transform the HTTP input for the following Create operation.
Here is the code for the same
Response Body
- hasErrors: true if an error occurred while creating a record; false otherwise
- results:
- Contains reference ID and new record ID of each record if success
- Contains only the reference ID of the record that caused the error, error status code, error message, and fields related to the error if there are any failures
Working Demo
Project XML File
Developer Resource
Happy Learning