Search code examples
mulemule-studiomule-el

Parsing for errors from Salesforce Connector in Mule ESB


I'm using Mule ESB 3.4 CE. I have this flow which uses Salesforce connector to create contacts. I want to parse the errors and do some action.

....
<sfdc:create config-ref="Salesforce" type="Contact" doc:name="Create Contacts">
     <sfdc:objects ref="#[payload]"/>
</sfdc:create>
<expression-component doc:name="Get Successful List">flowVars.listSuccess = ($ in payload if $.success=='true')</expression-component>
<expression-component doc:name="Get Failed List">flowVars.listErrors = ($.errors in payload if $.success=='false')</expression-component>

The result of the is as follows:

[[SaveResult  errors='{[1][Error  fields='{[1]Email,}'
  message='A contact with this email address already exists.'
  statusCode='FIELD_CUSTOM_VALIDATION_EXCEPTION'
 ]
 ,}'
 id='null'
 success='false'
]
, [SaveResult  errors='{[1][Error  fields='{[1]Email,}'
   message='A contact with this email address already exists.'
   statusCode='FIELD_CUSTOM_VALIDATION_EXCEPTION'
  ]
,}'
id='null'
success='false'
]
]

I was able to get the ids by the following expression component:

<expression-component doc:name="Get Failed List">flowVars.listIds = (Id in payload)</expression-component>

However, I was not able to the statusCode and message in case of errors. I tried various combinations, but I was not able to get it. For e.g. #[(message in (errors in flowVars.listErrors)]. How do I get the StatusCode and the message when Salesforce returns errors?


Solution

  • Your flowVars.listErrors will contain a list of Error arrays. Look at the following components for inspiration:

    <logger message="#[flowVars.listErrors[0][0].message]" level="INFO" doc:name="Logger"/>
    
    <scripting:component doc:name="Groovy">
       <scripting:script engine="Groovy"><![CDATA[
          flowVars.listErrors.each{it->it.each{it2->println('message:'+it2.message)}}
       ]]></scripting:script>
    </scripting:component>