Search code examples
sql-servermuledataweavedate-conversionmule-el

Query parameter date is changed to string in Mule


I have a query parameter called modifiedDate defined as date-only in the RAML file but when I look in the Mule debugger I see the modifiedDate 2001-10-10 as a string data type.

RAML

...
queryParameters:
   modifiedDate:
   type: date-only
   example: "2001-10-10
...

This is causing me a problem as when I call SQL Server stored procedure it is returning the error "Cannot convert NVARCHAR to Date".

I need to pass the modifiedDate to SQL Server in the format YYYY-MM-DD and as a Date datatype for this to work but I am also have problems converting it to a date datatype in Mule.

How can I change 2001-10-10 into a date datatype and keep the value the same?

I am using Anypoint Studio 6.2.2 and Mule 3.8.3.

Thanks


Solution

  • There are a couple of ways to parse a string as a date in mule

    Dataweave:

    %dw 1.0
    %output application/java
    ---
    {
        "modifiedDate" :  inboundProperties.'http.query.params'['modifiedDate'] as :date
    }
    

    will work without having to specify the date format

    Groovy

    payload['modDate'] = 
        Date.parse("yyyy-MM-dd", message.getInboundProperty('http.query.params')['modifiedDate']);
    payload;
    

    Both of these will convert the value into a java.util.Date