DataWeave - Transformer Mule error "There is no variable named 'message'" - mule

How to display Outbound properties in mule via the DataWeave transformer?
I tried this:
%dw 1.0
%output application/json skipNullOn="everywhere"
---
{
test_property: message.outboundProperties.testProperty
}
but I get this error: There is no variable named 'message'.
Thanks.

invoke directly without using 'message' as shown below
test_property: outboundProperties.testProperty
or else define a flow variable and use it as below
flowvar1: flowVars.flowvar1

Related

Import xml namespace from custom Dataweave module

I'm trying to create a custom Dataweave module for centralizing my custom XML namespaces.
I followed the official document of Mulesoft:https://docs.mulesoft.com/mule-runtime/4.3/dataweave-create-module
it states that: "When you import a custom module into another DataWeave script, any functions, variables, types, and namespaces defined in the module become available for use in the DataWeave body".
So I was expecting that I could create a module (in modules folder) containing my namespaces like this: Namespaces.dwl
ns myNs1 http://namespaces/my1
ns myNs2 http://namespaces/my2
import that module in another Dataweave like this:
%dw 2.0
import * from modules::Namespaces
output application/java
---
{
body: {
myNs1#Response: {
outcome: 'ACCEPTED'
}
} write "application/xml"
}
But I got this error:
The prefix myNs1 has not been previously declared using ns
I'm running on Mule 4.3.0
As aled have pointed out, it might have been a bug or incorrect information in the docs. From what I can see, the namespaces are properly imported but it seems that prefixes are expected to be declared locally.
You can use below:
%dw 2.0
import * from modules::Namespaces
output application/java
var myNs1Local = myNs1 as Namespace
---
{
body: {
myNs1Local#Response: {
outcome: 'ACCEPTED'
}
} write "application/xml"
}
which will result to the expected output.
{
body: "<?xml version='1.0' encoding='UTF-8'?>\n<myNs1:Response xmlns:myNs1=\"http://namespaces/my1\">\n <outcome>ACCEPTED</outcome>\n</myNs1:Response>" as String {class: "java.lang.String"}
} as Object {encoding: "UTF-8", mediaType: "*/*", mimeType: "*/*", class: "java.util.LinkedHashMap"}
Notice here that what I used as the prefix is the declared variable (myNs1Local) but it still write the prefix as referenced in Namespace.dwl

How to special character selector field in dataweave?

I am transforming a JSON input to XML output and I have a field with name Antigüedad with special character in it. I am following the below dataweave. XML is unable to generate this field. Can you please let me know where I am going wrong ?
%dw 1.0
%output application/xml skipNullOn="everywhere", encoding="UTF-8"
---
Acceptor #("Antigüedad": payload.Acceptor."Antigüedad")
I don't have sample data but this works for me:
%dw 1.0
%output application/xml skipNullOn="everywhere", encoding="UTF-8"
%var data = Acceptor #("Antigüedad": "Antigüedad"): {
}
---
Acceptor #("Antigüedad": data.Acceptor.#"Antigüedad"): {
}
Set
Content-type: application/json; charset=utf-8
in HTTP headers.

Mule Dataweave: How to dynamically map HTTP response(JSON) to XML output

I am building an application, where I have to hit a restful interface, and pass a specific section of the response to the UI.
The response model is quite huge with a lot of fields(String, array of objects, object,number datatypes), so using manual transformation is a big pain.
Is there a way to map the section of the response to a variable and directly send it to the UI.
I tried,
%dw 2.0
%output application/xml
%var UserAcct= payload.UserResponse.UserDetailsResp.UserAccounts
---
{
User: {
"UserAccount": {
Account: UserAcct
}
}
}
This doesn't work because, the payload has List, Array of Objects etc in the response and mapping to a variable throws an error. Is it possible to send the section payload.UserResponse.UserDetailsResp.UserAccounts directly in dataweave?? Please help.
It will be more helpful if you add Input payload, error and XML output.
Following is the code just by assuming your scenario. You can give this a try:
%dw 2.0
output application/xml
---
{
User: {
"UserAccount": {
(payload.UserResponse.UserDetailsResp.UserAccounts map {
Address:{
<XMLFieldName>: $.<respectiveJSONFieldToMap>
....
}
})
}
}
}

Mulesoft : Could not find a transformer to transform "SimpleDataType{type=java.util.LinkedHashMap

I am trying to get the data from Netsuite and save it in the salesforce object using Mulesoft Dataweave element alongwith When-Otherwise as per the snippet below, but gives me error:
Could not find a transformer to transform "SimpleDataType{type=java.util.LinkedHashMap, mimeType='/', encoding='null'}" to "CollectionDataType{type=java.util.List, itemType=java.lang.Object, mimeType='/'}".
Not sure how to convert Map to List as I am new to Mulesoft.
If I dont use When-Otherwise, it gives:
"Cannot coerce a :null to a :string error" if the value coming from Netsuite is null.
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Some_Id__c: payload.customFieldList.customField[0].StringCustomFieldRef__custbody_reference_id as :string when payload.customFieldList.customField[0].StringCustomFieldRef__custbody_reference_id != null otherwise ""
}
]]></dw:set-payload>
</dw:transform-message>
If you need a list, wrap it in square brackets. Also, try using default instead:
[
{
Some_Id__c: payload.customFieldList.customField[0].StringCustomFieldRef__custbody_reference_id as :string default ""
}
]

How to do mapping as arraylist of maps for below using dataweave mule

How to do mapping for below data.
ParameterMap{[card=[15242424211], phone=[54545454545]]}
This data is dynamic which is coming from http request as part queryparameters. I want to form query params in the form #[{'p1':'v1', 'p2':'v2'}] dynamically
for example into [{'card': '15242424211','phone':'54545454545'}]
i.e Array of maps(application/java) Using Dataweave in mule
can you please help on this
use following code
%dw 1.0
%output application/json
---
inboundProperties."http.query.params" mapObject {
($$) : $[0]
}
With this output will be {"card": "15242424211","phone":"54545454545"} you can wrap it under array if required by using
%dw 1.0
%output application/json
---
[inboundProperties."http.query.params" mapObject {
($$) : $[0]
}]
This will produce output as [{"card": "15242424211","phone":"54545454545"}]
Please refer org.mule.module.http.internal.ParameterMap for details of HTTP params.
Hope this help.
Update:-
Please use following for setting query parameters for HTTP outbound request.
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
inboundProperties."http.query.params" mapObject {
($$) : $[0]
}
]]></dw:set-payload>
</dw:transform-message>