Multiple conditions in the Choiceset - mule

I am trying to check multiple fields on the Choiceset using the Logical AND OR but it is throwing error.Below is the expression I tried
not isEmpty(vars.quoteLineItemId) AND (not isEmpty(payload.PhaseLevelItemNumber) OR not isEmpty(payload.PhaseLevelItemName)
And the error is like Unexpected end of input, expected ')' for the enclosed expression. (line 1, column 124):
I even tried below expression but no luck
vars.quoteLineItemId!=empty AND (payload.PhaseLevelItemNumber!=empty OR payload.PhaseLevelItemName!=empty)
Any help is greatly appreciated

Please make sure to provide sample input and expected output when you ask a question.
Payload
{
"PhaseLevelItemNumber": "Hello world!",
"PhaseLevelItemName": " Hello Mars"
}
Script
%dw 2.0
output application/json
var quoteLineItemId = "asdasdad"
---
not (isEmpty(quoteLineItemId) and ((not isEmpty(payload.PhaseLevelItemNumber) or not isEmpty(payload.PhaseLevelItemName))))

Related

Transform string to Date

Please I try to convert string to Date in anyoint studio but I get this error in Avanced rest client "Cannot coerce String (22/03/2012) to Date, caused by: Text '22/03/2012' could not be parsed at index 2.
please can I some one help me .
Thanks advanced .
It looks like you didn't use the right format for the conversion. You need to set the Date format if it is not the default.
Example:
%dw 2.0
output application/java
var inputString="22/03/2012"
---
inputString as Date { format:"dd/MM/yyyy" }

compare with properties file values with local variable in dataweave

In below dataweave code, comparision is not working properly for me.
Properties file key and value as below :
domain=Sales Domain, Retail Domain
Dataweave code :
%dw 2.0
output application/json
var test = 'Sales Domain'
---
{
result: if(upper(test) != (upper(Mule::p('domain')))) "Not equal" else ("equal")
}
Could you please help on this ?
The property domain contains the string Sales Domain, Retail Domain. The comparison with the variable test which contain the string Sales Domain. Obviously both strings are different, even converting to uppercase (upper()).
If you want to check if one string contains the other you could use the contains() function instead.
In DataWeave there is not a not-equal operator to compare the values. You can use the not operator to negate the result of an expression.
For example:
%dw 2.0
output application/json
var test = 'Sales Domain'
---
{
result: if(upper(Mule::p('domain')) contains upper(test)) "contains" else "not contains"
}

Datawave extract string using regexp

I'm trying to extract a part of a string using a regular expression:
\[CODE\] \((.*?)\)
Given the string [CODE] (ABC-212) Rest of the title this match (https://regexr.com/557qd)
I used this regexp in my current java application, and now I'm trying to transform this on a Mule App.
Reading the documentation I see that I need to use a Transform, so I setup like this:
%dw 2.0
output application/json
---
vars.subject match(/\[CODE\] \((.*?)\)/)
I stored the string on the var called subject.
Using that regexp on Mule, do not works.. but in my java application does. What I'm doing wrong ?
DataWeave doesn't seem to have a global search flag (the g at the end of the original regex). This means the regular expression has to match the entire string. You use the parenthesis to indicate capture groups that will be returned additionally to the matched string.
Script:
vars.subject match(/^\[CODE\] \((.*)\).*/)
Output:
[
"[CODE] (ABC-212) Rest of the title",
"ABC-212"
]
Match in DataWeave returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex. And in your case "Rest of the title" was not matching as per per Regex provided by you.
%dw 2.0
output application/json
var subject="[CODE] (ABC-212) Rest of the title"
---
//Catch here is, Regex should be fully matching as per input,
{
matchesOnly:subject matches /(\[CODE\]) \((.*?)\)[\w\W]*/,
matchOnly: subject match /(\[CODE\]) \((.*?)\)[\w\W]*/
}

How to generate dynamic XML Response in Mule4?

In Mule4, I need to convert json sample data in to dynamic XML format, I have tried with dataweave(2.0) field mapping, getting null values. Does anyone can help me on this?
If the question is just asking out to build up XML output from JSON input, that is a pretty open-ended question. What do you want to evaluate dynamically? You could, for example, use part of the payload to set values in the DataWeave expression.
There is a more difficult version of this question: how to dynamically evaluate DataWeave code constructed into an input string, where this string could be read from various script files, or even constructed in-line from some input data (payload, attributes, or variables).
Here is another example covered in our MuleSoft DataWeave training course at http://training.mulesoft.com.
You can use a Dynamic Evaluate component to dynamically evaluate a constructed DataWeave expression string. Here is an example that replaces the uName parameter with a dynamic value.
Also, the expression is configured to read in different script files based on some condition:
output application/json
---
do {
var choice = attributes.queryParams.script default "NO_SCRIPT"
---
if(choice == "NO_SCRIPT")
"output application/json --- {result: 'NO SCRIPT ENTERED'}"
else if(choice == "script1") vars.script1
else if (choice == "script2") vars.script2
else read(choice)
}
Here are two example scripts that substitutes values for uName and produce different types of output (XML vs. JSON).
This is script1:
output application/xml
---
root: { message: "order "
++ attributes.queryParams.orderid
++ " has been received from "
++ uName, items: payload.items}
This is script2:
output application/json
---
root: { message: "Order2 "
++ attributes.queryParams.orderid
++ " has been received from "
++ uName, items: payload.items}
Notice that this example is dangerous. It lets the web client inject any DataWeave code into the Mule flow, so this example should never be copied into production code, but it does demonstrate the ability to run any DataWeave code passed into a Mule application.

Mule ESB: How to do Condition checking in Datamapper using Xpath

i'm facing issue in xpath-I need do a check two attribute values, if the condition satisfies need to do hard code my own value. Below is my xml.
I need to check the condition like inside subroot- if ItemType=Table1 and ItemCondition=Chair1 then i have to give a hard coded value 'Proceed'( this hard coded value i will map to target side of datamapper).
<Root>
<SubRoot>
<ItemType>Table1</ItemType>
<ItemCondition>Chair1</ItemCondition>
<ItemValue>
.......
</ItemValue>
</SubRoot>
<SubRoot>
<ItemType>Table2</ItemType>
<ItemCondition>chair2</ItemCondition>
<ItemValue>
.......
</ItemValue>
</SubRoot>
....Will have multiple subroot
</Root>
I have tried to define rules as below, but it is throwing error
Type: String
Context:/Root
Xpath: substring("Proceed", 1 div boolean(/SubRoot[ItemType="Table1" and ItemCondition="Chair1"]))
But it is throwing error like
net.sf.saxon.trans.XPathException: Arithmetic operator is not defined for arguments of types (xs:integer, xs:boolean)
Is there any other shortcut way to perform this.Could you please help me, i have given lot more effort. Not able to resolve it. Thanks in advance.
I am not sure where you are applying this but the XPath expression you are looking for is:
fn:contains(/Root/SubRoot[2]/ItemCondition, "chair") and fn:contains(/Root/SubRoot[2]/ItemType, "Table")
So here is an example returning "Proceed" or "Stop" as appropriate:
if (fn:contains(/Root/SubRoot[1]/ItemCondition, "Chair") and fn:contains(/Root/SubRoot[2]/ItemType, "Table")) then 'Proceed' else 'Stop'
To implement the above condition , i was initially tired to do in xpath, gave me lot of error. I have implemented by simple if else condition in script part of data mapper
if ( (input.ItemType == 'Table') and (input.ItemCondition == 'chair')) {
output.Item = 'Proceed'}
else {
output.Item = 'Stop '};
Make sure about your precedence. Example, Here in the xml structure( or converted POJO) ItemType has to be checked first then followed with ItemCondition.
&& not seems to be working for me, change to 'and' operator
If you were first time trying to implement the logic. It may help you.