Skip to content

Instantly share code, notes, and snippets.

@atskimura
Last active February 26, 2024 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atskimura/6f33086e745b1d222efe294deafa1770 to your computer and use it in GitHub Desktop.
Save atskimura/6f33086e745b1d222efe294deafa1770 to your computer and use it in GitHub Desktop.
%dw 2.0
input records application/java
output application/json
---
{
users: records map (record) ->
record
filterObject (not $$ ~= "Id")
update {
case createdDate at .CreatedDate -> ((createdDate >> "UTC") as String { format: "hh:mm:ss a, MMMM dd, uuuu" })
}
}
List<Contact> data = [SELECT FirstName, LastName, CreatedDate FROM Contact ORDER BY LastName ASC LIMIT 5];
Assert.isFalse(data.isEmpty(), 'Contacts required for input data');
DataWeave.Script script = new DataWeaveScriptResource.jsonDateFormat();
DataWeave.Result result = script.execute(new Map<String, Object>{ 'records' => data });
// Note that DW uses Java 8 java.time.format, so uuuu is the year. vs. yyyy for Apex.
String expected = '{\n'+
' "users": [\n'+
' {\n'+
' "FirstName": "John",\n'+
' "LastName": "Doe",\n'+
' "CreatedDate": "' + data[0].CreatedDate.formatGMT('hh:mm:ss a, MMMM dd, yyyy') + '"\n'+
' }\n'+
' ]\n'+
'}';
Assert.areEqual(expected, result.getValueAsString(), 'Expected custom DateTime format in UTC');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment