'How to store the current date and message id in Mule 4 variable?

In my case, I'm doing a migration from Mule 3 to Mule 4.

I want to store the current date and message id in mule 4 variables.

Date formate is current date in "yyyyMMddHHmmssSSS"

Message_id is date ++ (random id (size is 6 characters))

code in mule 3:

<set-variable variableName="messageDateF" value="#[server.dateTime.format('yyyyMMddHHmmssSSS')]" doc:name="set messageDateF"/>
<set-variable variableName="messageId" value="#[flowVars.messageDateF]#[UUID.randomUUID().toString().replace('-', '').substring(0,5)]" doc:name="set messageId"/>

Expected output:

20220202024435724ddebd

Please assist me.



Solution 1:[1]

Hello In mule4 we use DWL so you can replace your code in Mule 4 using transform message and set output as variable with name "messageId"

Note: I am using 4.4.0 mule runtime, as substring Introduced in DataWeave version 2.4.0. as per official document link

%dw 2.0
import * from dw::core::Strings
output application/json
var messageDateF = now() as String {format: "yyyyMMddHHmmssSSS"}
var messageId= messageDateF ++ "" ++ substring ((uuid() replace ("-") with ""),0,5)
---
messageId

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1