convert dateTime to salesforce required dateTime format in mule 4 - mule

For the dateTime "2022-11-29T19:12:21Z" getting INVALID_TYPE_ON_FIELD_IN_RECORD error from salesforce. So need to convert it in correct format that Salesforce accept.
Input: "2022-11-29T19:12:21Z"
Expected output: 2022-11-29T19:12:21.000+0000
I tried this script:
{
"lastDateTime": "2022-11-29T19:12:21Z" as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSS"}
}
Please help to get the expected output format.

A DateTime must have a timezone so you can not just parse the input string into it directly. The input also misses milliseconds so it can not be parsed with the milliseconds format .SSS.
You can convert to a LocalDateTime first, then add a timezone (+0:00), then format to string with milliseconds and timezone. A DateTime must have a timezone so you can not just parse the input
%dw 2.0
output application/json
---
{
"lastDateTime": ("2022-11-29T19:12:21Z" as LocalDateTime {format: "yyyy-MM-dd'T'HH:mm:ss'Z'"} ++ |+00:00|) as String {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"}
}

Related

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"
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.

Date to DateTime conversion in DataWeave 2

Input date is coming in this format "2022-04-30" and I want to convert it to date time and time should be zeros like 2022-05-15T00:00:00Z.
How it can be achieved using DataWeave 2.0?
Convert the input string to a Date, then add the time to create a DateTime output. Then just convert to String. The default pattern seem to be adequate that you need.
%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
(sourceDate as Date {format: "yyyy-MM-dd"} ++ |00:00:00Z|) as String
Output:
"2022-04-30T00:00:00Z"
If you want the result as String you can just Append "T00:00:00Z" to your date string
%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
sourceDate ++ "T00:00:00Z"
And if you want a DateTime Object you can just coerce it to DateTime using as keyword. It will work as your date string is already in the required format.
%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
(sourceDate ++ "T00:00:00Z") as DateTime

formatting date in mule 4 Dataweave

I am using now() to get current datetime in Dataweave, I would like to format it so special character are removed and I get plain format like this 20200723T1720334450200
Try
now() as String {"format": "yyyyMMdd'T'HHmmssSSSSSSS"}

Mule changing the date format

I am trying to convert date from "dd-MM-yyyy" format to "dd/MM/yyyy"
dob as Date {format: \"dd-MM-yyyy\"} as String {format :\"dd\/MM\/yyyy\"}
I am getting a dataweave parsing error. Please help
dob as Date {format: "dd-MM-yyyy"} as String {format :"dd/MM/yyyy"}

Date format is not getting displayed

I have referred to solutions provided for date in yyyy-MM-dd format. After providing the format also, I am getting date in default format. Please help.
code:
Dim returndate As String
returndate = DateTime.Now.ToString("yyyy-MM-dd")
Dim oDate As DateTime = DateTime.ParseExact(returndate, "yyyy-MM-dd", CultureInfo.InvariantCulture)
As it stands I'm guessing your output of oDate is 27/01/2017 00:00:00.
What you want to do is this:
Dim returndate As String = DateTime.Now.ToString("dd/MM/yyyy")
Dim oDate As DateTime = DateTime.ParseExact(returndate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Debug.Print(oDate.ToString("yyyy-MM-dd"))
Send the returndate into DateTime.ParseExact with the format of "dd/MM/yyyy" and then format oDate to the desired output, in your case "yyyy-MM-dd".
This is a screenshot of the code and output:
Dates don't inherently have a format. The format you pass to ParseExact is the format it expects of the first parameter, in your case returndate. It's not the output format. Have a look over the DateTime.ParseExact documentation:
format
A format specifier that defines the required format of s. For more information, see the Remarks section.
Looking at the Remarks section:
The DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) method parses the string representation of a date, which must be in a format defined by the format parameter. It also requires that the date and time elements in s appear in the order specified by format. If s does not match the pattern of the format parameter, with any variations defined by the style parameter, the method throws a FormatException.