Mule 4-Salesforce Composite connector- self notes
we wanted to create an account, a contact for this account, and also create a user and associate this account/contact with this user.
To connect from Mule to salesforce, we need to first create a connected app in salesforce so that we can get the consumer Key and Consumer secret to connect to Salesforce.
Also we should have a user account/pwd in salesforce.
You should also have a security token associated with your account (you can generate one. refer here)
Next,
In Mule, I have used mule 4 salesforce composite connector version 2.7.0, and selected operation “Execute composite request” with the following json request. And it has created all the stuff I wanted
{
"allOrNone": true,
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v50.0/sobjects/Account",
"referenceId": "NewAccount",
"body": {
"Name": "Composite Request New Account ravi2"
}
},
{
"method": "GET",
"url": "/services/data/v50.0/sobjects/Account/@{NewAccount.id}",
"referenceId": "NewAccountInfo"
},
{
"method": "POST",
"url": "/services/data/v50.0/sobjects/Contact",
"referenceId": "NewContact",
"body": {
"lastname": "TestCommunityravi2",
"firstname": "TestRegOrgravi2",
"Email": "test@test.com",
"accountId": "@{NewAccountInfo.Id}"
}
},
{
"method": "GET",
"url": "/services/data/v50.0/sobjects/Contact/@{NewContact.id}",
"referenceId": "NewContactInfo"
},
{
"method": "POST",
"url": "/services/data/v50.0/sobjects/User",
"referenceId": "NewUser",
"body": {
"lastname": "@{NewContactInfo.LastName}",
"firstname": "@{NewContactInfo.FirstName}",
"Email": "@{NewContactInfo.Email}",
"profileid": "00e5m000000Ls7cAAC",
"contactid": "@{NewContactInfo.Id}",
"Username": "myravitest2@test.com",
"Alias": "ravitst2",
"TimeZoneSidKey": "Australia/Sydney",
"LocaleSidKey": "en_US",
"EmailEncodingKey": "ISO-8859-1",
"LanguageLocaleKey": "en_US"
}
}
]
}
In the connector config, I’ve used “OAuth Username Password” option with following config
Some links helped me to get started as this was my first salesforce integration
https://dzone.com/articles/salesforce-composite-connector-in-mule-4
Salesforce REST API with Composite Resources
https://docs.mulesoft.com/salesforce-composite-connector/2.7/
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/requests_composite.htm
Get exception payload (Not error description)
in some cases when you call a service it may throw some 500 error and may return some payload
{ "Message": "Failed to submit request" }
or could be something like
"Failed to submit request"
Or anything else
to access this payload in your exception handler you can use
#[error.errorMessage.payload]
if you want to use it in dataweave with other string objects, better use it as , this will convert the exception payload to string
error.errorMessage.payload.^raw
NOTE: This exception payload object will not be available always, for example if the exception happens due to internal error in you flow(before calling any other service for example), this is an exception which happened with in your flow, so there’re won’t be any exception payload available.
Further References https://www.ricston.com/blog/mule-4-error-handling-review/
payload.^raw usage
payload.^raw can be used to convert your payload to a raw string to be used as a string value anywhere
for example, One of the response payload is json and you want to concatenate this json response as a string to another text like below
“hello” and the json response is { “name”: “test”}
when you use dataweave code like
%dw 2.0 output application/json --- Name: "hello" ++ payload
this will throw an error of “cannot coerce Object to String”
So basically payload.^raw can be used to convert your payload to a raw string
%dw 2.0 output application/json --- Name:  "hello" ++ payload.^raw
this will result in output of
Name: "hello{ \"name": \"test\"}"
Get the retried count of a message in ActiveMQ Queue in Mule 4
ActiveMQ provides several properties that can be used when we need them. You can look at the available properties here
There properties will be made available in Mule flow when the message is dequeued using JMS connector.
We have had a requirement to handle retry mechanism when the dequeued message fails and after x retry attempts, we had some business logic to be done.
So how do we know what’s the current retry attempt count?
We can rely upon the property JMSXDeliveryCount provided by ActiveMQ. This property initial value will be 1 and will be incremented by 1 every time it is retried
You can read this attribute in Mule 4 like below
attributes.properties['jmsxProperties'].'jmsxDeliveryCount'
following is a snap of the dubugger mode that shows list of properties available in mule flow when a message is dequeued from ActiveMQ.
if you notice, these properties are not part of headers. They’re under “properties/jmsxProperties” (some other ActiveMQ properties will be available under headers too)
-
Archives
- October 2023 (1)
- October 2022 (2)
- October 2021 (2)
- February 2021 (5)
- January 2021 (4)
- December 2020 (3)
- October 2020 (2)
- August 2020 (1)
- June 2020 (1)
- April 2020 (1)
- February 2020 (1)
- December 2019 (7)
-
Categories
-
RSS
Entries RSS
Comments RSS