Search code examples
salesforcensdateformatterdate-formatdataweavemulesoft

Update Salesforce: Date format Dataweave


I have a field dateLastPaid into a JSON Payload. The type of this field is a date in this format 2019-05-10T00:00:00.000-0300. The Salesforce API expects a date-only field but I can't convert it.

I tried to use payload.dateLastPaid as :date{format: "yyyy-MM-dd"} but it still add the time.

Can you help me?


Solution

  • The problem is trying to format a date. You can format a string, or use a format to parse a string, but in DataWeave, or Java, dates and datetimes don't have format. It works after converting the date from the initial string format to a datetime before trying to format it back into the desired format.

    I'm assuming the date is in a JSON string attribute, as you didn't show an actual example.

    Input:

    {
      "dateLastPaid" :  "2019-05-10T00:00:00.000-0300"
    }
    

    DataWeave script:

    %dw 1.0
    %output application/json
    ---
    {
      date: ( payload.dateLastPaid as :date {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"})  as :string {format: "yyyy-MM-dd"} 
    }
    

    Output:

    {
      "date": "2019-05-10"
    }