MuleSoft, Salesforce UPSERT
Let's take a look at how to create a simple global HTTP connector to receive requests.
Join the DZone community and get the full member experience.
Join For FreeHTTP Config:
MuleSoft has Salesforce connectors that facilitate CRUD operations for Salesforce objects. Take a look at MuleSoft, Salesforce Request and MuleSoft, Salesforce POST for more information
HTTP Connector:
We are going to create a simple global HTTP connector to receive our requests:
To make the example simple, we are going to create the following flow:
- /contacts PUT
HTTP config:
Salesforce Connector:
Drag and drop the Salesforce connector right after the GET HTTP:
- Select Salesforce Basic Authentication
- Fill out the username, password, and security tokens with Salesforce credentials.
- Click validate configurations to check the connection.
Salesforce Upsert:
In upsert operation, Salesforce internally validates the data based on Object's Id or External ID. So, upsert takes a little bit longer time than insert or update. Using the upsert operation, you can either insert or update an existing record in one call.
In this example, we are going to use email as the External ID Field Name. In other words, the email passed in the paylod will be validated whether it exists, if so, the object will be updated, otherwise, a new object will be created.
Drag and drop a transformer connector between HTTP and Salesforce. Doing that, you can make use of the data sense to help you to make the transformations.
We are going to use the following payload to create a contact:
{
"last_name": "Obama",
"first_name": "Barack",
"cel": "555 6987",
"personal_email": "bo@whitehouse.com"
}
The MuleSoft Transformer should look like this:
%dw 1.0
%output application/java
---
[{
LastName: payload.last_name,
FirstName: payload.first_name,
Phone: payload.cel,
Email: payload.personal_email
}]
NOTE: Pay special attention to %output, as it should be application/java
Add another transformer connector after the Salesforce connector. It will be responsible for transforming the resulted Salesforce message into JSON format.
The transform message should look like this:
%dw 1.0
%output application/json
---
{
id: payload[0].id,
success: payload[0].success
}
UPSERTING a Contact
Start the Mule project from Postman, make a post request, and pass the contact JSON as a parameter:
The JSON response should indicate if the contact was properly created or updated.
Opening Salesforce, we can find our created Contact (Barak Obama).
If you sent another contact, keeping the same email but changing other fields, you can check the contact updated.
Opinions expressed by DZone contributors are their own.
Comments