Reputation: 59
how to use IF ELSE condition in Dataweave 2.0? I am trying to do a condition around one of the Payload Data Elements but its throwing error when I execute it.
Unable to resolve reference of method
Unable to resolve reference of attribute
Am I missing something?
XML >
<payments>
<payment>
<custom-method>
<method-name>AdyenComponent</method-name>
<custom-attributes>
<custom-attribute attribute-id="adyenPaymentMethod">iDEAL</custom-attribute>
</custom-attributes>
</custom-method>
<amount>145.99</amount>
<processor-id>Adyen_Component</processor-id>
<transaction-id>851603387831889A</transaction-id>
<custom-attributes>
<custom-attribute attribute-id="Adyen_log"></custom-attribute>
<custom-attribute attribute-id="authCode">Authorised</custom-attribute>
</custom-attributes>
</payment>
</payments>
Dataweave code
%dw 2.0
output application/json
ns ns0 http://www.demandware.com/xml/impex/order/2006-10-31
---
[{
Ascent_FPL__Payment_Method__c : if ((payload.order.payments.payment.custom-method.custom-attributes.*custom-attribute filter(item) -> (item.@"attribute-id" == "adyenPaymentMethod")) [0] =="iDEAL") "iDEAL" else (payload.ns0#order.ns0#"custom-attributes".*ns0#"custom-attribute" filter(item) -> (item.@"attribute-id" == "creditCardType")) [0]
}]
Upvotes: 1
Views: 612
Reputation: 4303
%dw 2.0
output application/json
ns ns0 http://www.demandware.com/xml/impex/order/2006-10-31
---
[{
Ascent_FPL__Payment_Method__c : if ((payload.order.payments.payment."custom-method"."custom-attributes".*"custom-attribute" filter(item) -> (item.@"attribute-id" == "adyenPaymentMethod")) [0] =="iDEAL") "iDEAL" else (payload.ns0#order.ns0#"custom-attributes".*ns0#"custom-attribute" filter(item) -> (item.@"attribute-id" == "creditCardType")) [0]
}]
As well you have an extra custom-attributes towards the end of the xml which you need to remove.
Upvotes: 2