I want to enrich my message (POJO) properties from original payload stored in flow variable
<set-variable variableName="SupplierRequest" value="#[payload]" doc:name="SupReq"/>
<flow-ref name="GetSupplierRequestDetail" doc:name="GetReqData"/>
<set-variable variableName="SupplierRequestData" value="#[payload]" doc:name="SupReqData"/>
In above code, I need couple of SupplierRequestData POJO properties to be set with properties from SupplierRequest POJO.
Do I need to write custom transformer or any other solution?
Ideally you should use the enritcher. But given that you already have the original payload in a flow variable you could just use an expression component as an expression transformer would imply a transformation from A to B while this is modification of A with B:
<expression-component><![CDATA[message.payload.propertyName = flowVars.myOrigPayload.myProp]]></expression-component>
Related
I am using the below code to convert the input payload string to json in mule. The below code sometimes working and sometimes not. its not working on standalone and working on studio. Not able to nail down the exact cause for it. but based on the loggers that i see that the property value is coming null after the expression statement. i am suspecting this could be with the jar that's getting used here. i am still digging further on it.
<logger message="input: #[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="#[payload.'data']" mimeType="application/json" doc:name="Set Payload" encoding="ISO-8859-2"/>
<logger message="createConnection: #[payload]" level="INFO" doc:name="Logger"/>
<expression-component doc:name="Expression"><![CDATA[String input = payload;
payload = new org.json.JSONObject(input);
]]></expression-component>
<logger message="before json to object: #[payload.con_id] #[payload.'con_id']" level="INFO" doc:name="Logger"/>
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
Input JSON:
data: {"name":"QA_tst2","description":"tst","con_id":10,"con_connection_id":null,
"verticalParam":[{"param_value":"abc","param_name":"Host"},{"param_value":"21","param_name":"Port"}],"CON_CATEGORY_NAME":"File"}
I don't think that notation will work for JSONObject, try using
payload.get('con_id')
as per the javadoc: https://stleary.github.io/JSON-java/org/json/JSONObject.html.
The reason this won't work with the notation you have tried, is that Mule supports that notation for Maps, and org.json.JSONObject does not implement java.util.Map. You could try using javax.json.JSONObject instead, which will support that notation.
I have figured out the current issue. if there is any logger added to fetch the properties from the payload right after the expression component then its screwing up further. if you just remove the logger that was added after the expression component then after json to object conversion, i am able to fetch the values. that solves the current issue. but i would like to understand the difference between fetching the properties #[payload.con_id] vs #[payload.'con_id']. i can start a separate conversation for the same.
In For Each introdution we have: "The Foreach scope splits a collection into elements and processes them iteratively through the processors embedded in the scope, then returns the original message to the flow."
I would like to know if we have a form to return the original message to the flow when on the middle of the flow we have a processing with new message (payload) similar For Each works, but it is not a case to use For Each!
Example:
Flow (payload AA) -> ["Sub" flow (payload BB)] -> Flow (payload AA)
Is there a form to do this?
The simplest way is to use an enricher that stores the result of the sub-flow in a flowVar so that the original payload is not modified:
<enricher target="#[flowVars.someSubFlowResult]">
<flow-ref name="mySubFlow" />
</enricher>
Or alternatively manually store the original payload into a flow variable on entering the flow and at the end of the flow set the payload back to the original payload using the flow variable. For example:
<set-variable variableName="originalPayload" value="#[payload]" />
<!-- do some other processing -->
<set-payload value="#[flowVars.originalPayload]" />
I am using a Message Enricher to call a web services and return what a part number is for the external data source. I am saving that payload into a Session Variable. I am then using a Lookup Table from within a Datamapper to send the current payloads' part number to be referenced against the external data source (using xpath). I am able to invoke the Lookup and pass the local variable but the payload that was saved into the Session Variable is not being passed through to the Lookup Flow, so my xpath query will not work.
Here is the Session Variable and Datamapper
<flow>
<enricher target="#[sessionVars['SesVar']]" doc:name="Message Enricher">
<flow-ref name="query-line-details-erpFlow" doc:name="query-line-details-erpFlow"/>
</enricher>
<logger message="Session Var: #[sessionVars['SesVar']]" level="INFO" doc:name="Logger"/>
<data-mapper:transform config-ref="XML_To_XML" doc:name="XML To XML"/>
</flow>
Here is the Lookup Table logic
output.ExternalPart = (isnull(lookup(LookUpPart).get([input.LocalPart])) ? null : lookup(LookUpPart).get([input.LocalPart]).ExternalPart);
Finally here is the second flow where the Session Var should be accessed from
<flow>
<logger message="Spit out the var #[sessionVars.SesVar]" level="INFO" doc:name="Logger"/>
</flow>
From what research I have done, the Session Variable is not passing a Transport Barrier so it should be able to be referenced from this scope. I have also tried with flowVars also.
Any help would be greatly appreciated.
i want payload value like below in <http:outbound-endpoint>:
<set-variable variableName="url" value="#[json:url]" doc:name="Variable"></set-variable>
<set-variable variableName="payload" value="#[json:param]" doc:name="Variable"></set-variable>
<http:outbound-endpoint exchange-pattern="request-response" address="http://admin:admin##[url.substring(7)]" method="POST" doc:name="HTTP" password="admin" user="admin">
<set-payload value="action=start¶ms={input:#[payload]}&createTask=false&parts=all"/>
</http:outbound-endpoint>
but it is giving error.
thanks.
You can get rid of the error by replacing & with &
However, that is not your only problem.
payload is a reserved variable to hold the current payload. You can not use set-variable with variableName="payload", your setting will just be ignored when you call #[payload] later.
Your POST data would be something like action=start¶ms={input:yourdata}&createTask=false&parts=all. This is some kind of hybrid of JSON and HTTP GET syntax, and I doubt that this is what you are trying to achieve. If you want to send a POST request in key=val format, set a payload of type Map. If you want to send the request as JSON, set a payload with a JSON String, you have the object-to-json-transformer to help in Mule. If you want to have parameters in the URL, put them in the URL. But you can not mix these different syntaxes.
In order to set multiple values to a set payload transformer in mule we use
<set-payload value="#[{1000,1,1,1}]" doc:name="Set Payload"/>
can we assign multiple flow variables to a set payload transformer
<set-payload value="#[{flowVars['principal'],flowVars['years'],flowVars['rate'],flowVars['appid']}]" doc:name="Set Payload"/>
Or is there any other right way to do it
Thank you in advance
Yes, this will work just fine. Whether you can do something that looks prettier, would be up to your full flow configuration.
You can also use as following :-
<set-payload value="#[flowVars['principal']] #[flowVars['years']] ..." doc:name="Set Payload"/>