Loading

DataWeave Module-Level Variable Using now() Returns Same Value After Application Startup

Publish Date: Aug 7, 2026
Description
  • A DataWeave expression using now() inside a custom .dwl module always returns the same date/time value, regardless of when the request is made.
  • The date/time value returned matches the time the application was last deployed or restarted.
  • Redeploying or restarting the application temporarily resolves the issue, but the value freezes again at the new startup time.
  • The behavior is consistent across Mule Runtime versions.

Root Cause

This is expected DataWeave module behavior, not a product bug or runtime regression.

When a DataWeave module (.dwl file) is loaded, all module-level var declarations are evaluated on first access and their result permanently cached for the lifetime of the application. This means any dynamic expression — including now() — assigned to a module-level var is evaluated exactly once, at or shortly after application startup, and the same value is reused for every subsequent request.

// now() is evaluated ONCE at module load time
// and the same value is returned for all subsequent requests
var currentDate = now() as Date {format: "yyyy-MM-dd"}

This behavior is distinct from using now() inline in a transform body (after ---), which is re-evaluated on every request as expected:

%dw 2.0
output application/json
---
{
    // now() is re-evaluated on every request
    currentDate: now() as Date {format: "yyyy-MM-dd"}
}
Resolution
  • Step 1: Convert the module-level var to a fun. Unlike a var, a function body is re-executed on every invocation, ensuring now() returns a fresh, accurate timestamp with each request.

Before:

var currentDate = now() as Date {format: "yyyy-MM-dd"}
var currentDateTime = now() as DateTime {format: "yyyy-MM-dd'T'HH:mm"}

After:

fun currentDate() = now() as Date {format: "yyyy-MM-dd"}
fun currentDateTime() = now() as DateTime {format: "yyyy-MM-dd'T'HH:mm"}

 

  • Step 2: Update all call sites in consuming .dwl files to use function call syntax with ():
// Before — referenced as a variable
%dw 2.0
import currentDate from modules::CustomUtil
---

// After — invoked as a function
import * from modules::CustomUtil
---
currentDate()




Knowledge Article Number

005390666

 
Loading
Salesforce Help | Article