Search code examples
muledataweavemulesoft

Dataweave: Cannot coerce String to Datetime


So I need to transform this String : "2023-01-30T15:00:00+0100" to Datetime in order to send to SF.

I've tried like this :

StartDateTime: payload.planDate as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"} and instead of "DateTime" , "LocalDateTime". None of them seem to work.

Both give me this error : Cannot coerce String (2023-01-30T15:00:00+0100) to DateTime, caused by: Text '2023-01-30T15:00:00+0100'

Could you help me transform the String to DateTime?

Expected Result : "2023-01-30T15:00:00+0100"


Solution

  • The input data is a DateTime because it includes a timezone. Your format is incorrect because it has milliseconds and the input has not. Removing that should be OK.

    Example:

    %dw 2.0
    output application/json
    var planDate="2023-01-30T15:00:00+0100"
    ---
    planDate as DateTime {format: "yyyy-MM-dd'T'HH:mm:ssZ"}
    

    Output:

    "2023-01-30T15:00:00+0100"
    

    Note that the output is a string because the output format is JSON and JSON doesn't has a date type. If you output other in a different format you will see the DateTime output. If you intend to use the resulting DateTime for calculations in the flow then application/java is the best output format for performance.