简体   繁体   中英

Converting to SalesForce datetime format in Mule-3.8.4 using DataWeave 1.0

I am trying to convert a datetime column in a CSV to a salesforce datetime format to do an upsert. I tried expression like below in mule-3.8.4 dataweave1.0 but I am getting an error.

I tried the following:

First Try

Test_Date: "1/14/19 6:31 PM" as :localdatetime { format: "M/dd/yy h:mm a" } as :localdatetime { format: "YYYY-MM-DD'T'hh:mm:ssZ" })

Expected Output : 2019-01-14T06:31:00Z

Actual Output : Mon Jan 14 18:31:00 EST 2019

After Upsert in Salesforce, it looks like this: 2019-01-14T00:00:00.000+0000 . It is not saving the time.


Second Try

Test_Date: "1/14/19 6:31 PM" as :localdatetime { format: "M/dd/yy h:mm a" } as :localdatetime { format: "YYYY-MM-DD'T'hh:mm:ss" })

Expected Output : 2019-01-14T06:31:00

Actual Output : Mon Jan 14 18:31:00 EST 2019

After Upsert in Salesforce, it looks like this: 2019-01-14T00:00:00.000+0000 . It is still not saving the time.

What is the problem?

To format a date, the format pattern reading left to right, needs to match your expected output. So for your example:

2019-01-14T06:31:00Z

yyyy-MM-dd'T'HH:mm:ssZ

y is for year M is for Month(lowercase m is for minute

d is for day

H is for hour in 24 hr format. (Lowercase would be 12 be format)

S is for milliseconds

And Z is time zone There's more you can configure for time zone and milliseconds if needed.

Here are the salesforce date format info:

https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/datafiles_date_format.htm

More date format info:

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Have you tried sending it to Salesforce as a String ? Once something is of type LocalDateTime , there is no further formatting it. The formatting is solely for the purpose of parsing a String into a LocalDateTime or formatting the output of a LocalDateTime to a String . For example:

"1/14/19 6:31 PM" 
  as :localdatetime { format: "M/dd/yy h:mm a" }         // This parses the string as LocalDateTime
  as :localdatetime { format: "YYYY-MM-DD'T'hh:mm:ssZ" } // This doesn't do anything, it's already LocalDateTime

If you want to take one String representing a datetime and format it to a different String representing the same datetime, you could do this:

...
%var inputFormat  = "M/dd/yy h:mm a"
%var outputFormat = "yyyy-MM-dd'T'HH:mm:ss"
---
"1/14/19 6:31 PM" 
  as :localdatetime { format: inputFormat  } // Used to parse the input string
  as :string        { format: outputFormat } // Used to format the output string

Output of that last script should be "2019-01-14T18:31:00"

Finally below code worked. Mule Dataweave converted the following format(yyyy-MM-dd'T'HH:mm:ss.SSSZ) to java.util.Calendar Object.

Test_Date: "1/22/19 6:31 PM" as :localdatetime { format: "M/dd/yy h:mm a" } as :datetime { format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ" })

This is how the Payload looks after Transform Message in DataWeave(above step):

Test_Date=java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=0,WEEK_OF_YEAR=13,WEEK_OF_MONTH=5,DAY_OF_MONTH=22,DAY_OF_YEAR=84,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=4,HOUR_OF_DAY=18,MINUTE=31,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]

Output after Upsert in SalesForce : 2019-01-22T18:31:00.000+0000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM