Need to know how can we use below expression component in Mule 4 using DataWeave or any other method - mule

I am working on code migration and in that I am facing problem in migrating expression component of Mule 3 to Mule 4. I tried it using Transform Message Component but I am facing some errors in it. Can someone Please help me to migrate the below Script into Mule 4 using the correct component.
<expression-component doc:name="Expression"><![CDATA[if (flowVars.deletesVar != null) { flowVars.combinedArray.addAll(flowVars.deletesVar); } if (flowVars.insertsVar != null) { flowVars.combinedArray.addAll(flowVars.insertsVar); } if (flowVars.updatesVar != null) { flowVars.combinedArray.addAll(flowVars.updatesVar); } else{ flowVars.combinedArray=[]; }]]></expression-component>

Mule 3 expression language is MEL which is not supported in Mule 4. You need to migrate the expression to DataWeave 2.0 or migrate it to a Groovy script, or even Java code.
See the migration guide: https://docs.mulesoft.com/mule-runtime/4.4/migration-mel for more information.
Assuming that the 3 variables are arrays or null you can use this script and assign the result to variable combinedArray.
%dw 2.0
output application/java
---
(vars.deletesVar default []) ++ (vars.insertsVar default []) ++ (vars.updatesVar default [])
Note that the logic of the original script seems to be missing some conditions, but this should give a similar result.

Related

Transform connector fails in the Anypoint CloudHub

I have a transform action which has the logic like below
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
"quoteId" : vars.setQuoteOppRecIds.Id,
"productCode" : payload.ServiceTypeCode,
"axSequenceNumber" : vars.counter as Number,
"phaseLevel" : payload.PhaseLevel as Number,
"phaseLevelDescription": payload.PhaseLevelDescription,
"projectWeekStart" : payload.PhaseLevelProjectWeekStart as Number,
"projectWeekEnd" : payload.PhaseLevelProjectWeekEnd as Number,
"quantity": payload.Quantity as Number,
"invoicedSinceLast" : payload.InvoicedSinceLast as Number,
"unitPrice": (payload.UnitPrice replace "," with "") as Number {format: ".00"} as String {format: ".00"} as Number,
"oppId" : if (isEmpty(vars.inputPayload.Opportunity.OpportunityId)) (vars.oppID) else (vars.inputPayload.Opportunity.OpportunityId)
}
When debugging this transform locally it works without any issues but after deploying to CloudHub it throws error like
Unable to resolve module with identifier dw::util::Coercions. at 2 : 2
at org.mule.weave.v2.el.WeaveExpressionLanguage.compile(WeaveExpressionLanguage.scala:68)
at org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor.compile(DataWeaveExpressionLanguageAdaptor.java:143)
at org.mule.runtime.core.internal.el.DefaultExpressionManager.compile(DefaultExpressionManager.java:233
I am new to mulesoft I didnt capture this error when debugging locally but why does it throws when running the API in the CloudHub
The DataWeave package dw::util::Coercions used in the script was added to Mule since Mule 4.4.0. You are probably deploying in CloudHub to an earlier version of Mule that doesn't support it. Re deploy the application to Mule 4.4.0 and it should work.
Alternatively, since your script doesn't use any of the functions provided by that package you could just delete the import line.

Mule4 equivalent code for Mule3 mel/dw code

We are migrating a project from mule3 to mule4 and encountered below MEL or DW code after migrating using MMA. Any inputs on how to convert these into mule4 code is appreciated. There are variables which might confuse for eg. appendix, muleOverrideColumns etc.
mel:flowVars.appendix.put(flowVars.currentCode, new java.util.HashMap())
mel:flowVars.appendix.get(flowVars.currentCode).put(payload.Value, payload.Definition)
mel:flowVars.muleOverrideColumns.split("!")
mel:payload.split("!")
mel:flowVars.appendix.get(flowVars.currentCode).put(payload[0], payload[1])
mel:appendix.get(propertyKey).get(payload.getValue()) != null ? payload.setValue(appendix.get(propertyKey).get(payload.getValue())) : payload.setValue(payload.getValue())
mel:appendix.get("m3/m4").get(payload.getValue()) != null ? payload.setValue(flowVars.appendix.get("m3/m4").get(payload.getValue())): payload.setValue(payload.getValue())

How can I run MongoDB "adminCommand" in Mule

I need to get the list of MongoDB databases in Mule.
Here is the Mongo CLI command - db.adminCommand( { listDatabases: 1 } )
I tried below way and that doesn't worked
< mongo:execute-command config-ref="Mongo_DB" commandName="eval"
commandValue="db.adminCommand( { listDatabases: 1 } )" doc:name="Mongo
DB" />
I do not know Mongo specifics, however the connector needs a JSON document as the command which you can use Dataweave to build. Something like:
<mongo:execute-generic-command config-ref="Mongo_DB">
<mongo:command ><![CDATA[#[%dw 2.0
output application/json
---
{
listDatabases: 1
}]]]></mongo:command>
</mongo:execute-generic-command>

logging from inside dataweave

Just like we use
<xsl:message>
inside XSL transformer and
system.out.println
for datamapper, do we have any logging mechanism for dataweave ? If not a direct component, do we have any other alternate mechanisms to achieve logging from inside dataweave ?
In mule 3.8 you can do it like this ,mule allows logging in dataweave
%dw 1.0
%output application/json
---
{
result: log("Logging the array",[1,2,3,4])
}
you can refer the latest document for this here
You can take a look at my answer here - https://stackoverflow.com/a/36458835/5616671.
If you want to log every record that is being processed by dataweave map, you can change the filter function to return true always and log value before returning.
BTW, What type of logging you want to do?
For now the only way and best way to debug Dataweave is to use code on the lines given below.
Value: log("This is Debugvalue",flowVars.company)
You can replace the flowVars.company with any of the values you want to pring during the runtime of application.
Use the dataweave function log
Script
%dw 2.0
output application/json
var x = now()
---
log("Today is " ++ x)
Output
"Today is 2020-03-24T00:38:58.323Z"
Source:
https://docs.mulesoft.com/mule-runtime/4.2/dw-core-functions-log
At the current time (2021), you would be using DataWeave 2.x and there is a handy log function in the DW library.
You may use it like this:
%dw 2.0
output application/dw
var usermessage = "Bob was here"
---
log ("LOGGEDUSERMESSAGE",usermessage)
The output in the log will look like this:
INFO 2021-04-20 16:20 .... LoggingService$: LOGGEDUSERMESSAGE - "Bob was here"
In application however, log() resolves to the string version of the second argument. Or to say it another way, it passes the second argument along untouched, but it logs both the tag you provide in the first arg separated from the second arg by a dash.
Take note, this is not the logging level. It is an internal tag that the application owner can use to filter log entries.

Message.InboundProperty always return String - MULE - AnyPoint Studio - APIkit

im developing an API using RAML + MULE AnypointStudio (APIkit) . so in my RAML file i had defined a resourse like this.
/player
post:
queryParameters:
year: type: integer
place: type: string
Then, into AnyPoint Studio, after importing my .raml file i got a flow with the post method asociated. I use the MySQL Conector to insert in a data base. the problem resides in the query:
INSERT INTO Matches (day,place,max_players)
VALUES (#[message.inboundProperties.'day'],#[message.inboundProperties'place'],
#[message.inboundProperties.'maxPlayers'])
when i call #[message.inboundProperties.day] it returns a string, but i want an integer value.
im new at MULE, so it would be great if you can explain me how.
ty
All query parameters are treated as Strings. You can use MEL to convert to an int though. Here's an example suing Java in a Mel expression to parse the parameter to an int.
#[Integer.parseInt(message.inboundProperties.day)]