Skip to main content
Group

DataWeave in Apex

For collaboration and discussion of the ability to invoke DataWeave scripts from Apex for accessing and transforming data. The DataWeave Tutorial and Playground: https://developer.mulesoft.com/learn/dataweave/tutorial

First thing is: what is the correct entry on a csv file for datetime that would allow it to be transformed in the dw script?  

Then: what would be the correct formatting in the dw script to process that datetime value? 

 

I've tried numerous combinations of both and nothing is working. This is my latest attempt: 

%dw 2.0 

input records application/csv 

output application/apex 

--- 

records map(record) -> { 

-- some other fields -- 

 TestDateTime__c: (record.DT >> "UTC") as String {format: "hh:mm:ss a, MMMM dd, uuuu"}  

} as Object {class: "MyObject__c"} 

 

I've also tried: 

TestDateTime__c: record.DT as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"} 

and many others. 

 

But I'm just not getting the right combination of csv datetime and dw transformation. 

 

Has anyone been able to correctly process datetime for csv -> Apex?

3 answers
  1. Apr 30, 2025, 12:53 PM

    Hi @Sarah Kennedy

    datetime handling in DataWeave with CSV can definitely be frustrating — here’s what usually works well: 

    CSV Format Enter the datetime like this: 

    2025-04-30T14:45:00Z (This format is standard and includes the date, time, and timezone.) 

     

    DataWeave Script Format: 

     

    TestDateTime__c: (record.DT as DateTime {format: "yyyy-MM-dd'T'HH:mm:ssX"}) as String 

     

    Read the DT field as a DateTime 

    Match it to the ISO format in the CSV 

    Convert it into a string for Apex 

     

     

    Make sure there are no extra spaces or invalid characters in the CSV column always include the time zone (Z or +00:00) in the CSV. 

    Avoid formats like “hh:mm a, MMM dd” — they’re harder to parse reliably. 

     

    If you find this answer helpful please mark this as an accepted answer.

0/9000

Resources from the Salesforce Developers Ask Me Anything on Apex Updates + Innovations

 

I hope you joined us for the fantastic SFDevsAMA on Wednesday 29th November featuring @Mohith Shrivastava and @Dominic Foster. They loved answering your questions about Apex. 

 

If you missed it, don't worry - we recorded the series and our experts have provided some great resources that our experts for you to continue your learning! 

 

Still have questions? Post them in this thread for our experts!Resources from the Salesforce Developers Ask Me Anything on Apex Updates + Innovations I hope you joined us for the fantastic SFDevsAMA on Wednesday 29th November featuring and .

@DataWeave in Apex @Salesforce Apex Hours @Apex Transaction Finalizers @Generics in Apex @User-Mode Database Operations

 

#Ask An Expert #CommUpdates #Apex #Apex Class #ApexDevelopment #Apexhours

12 comments
  1. Jul 5, 2025, 3:03 PM

    Using libraries like ApexMocks is now super verbose because of the lack of generics. 

    With generics, this:

    private static final MyService myService = (MyService) apexMocks.mock(MyService.class);

    could be written as this:

    private static final MyService myService = apexMocks.mock(MyService.class);

     

    And mock verification would change from: 

     

    ((MyService) apexMocks.verify(myService)).doSomething();

    to

    apexMocks.verify(myService).doSomething();
0/9000

I have the following script: 

%dw 2.0 

input records application/csv 

output application/apex 

--- 

records map(record) -> { 

 IVP_First_Name__c: record.'First Name*', 

 IVP_Last_Name__c: record.'Last Name*', 

 IVP_Middle_Name__c: record.'Middle Name', 

 IVP_Date_of_Birth__c: record.'DOB*' as Date{format : "M/d/yyyy"}, 

... 

 

This works fine except when there is no date entered on the CSV. It just throws an obscure error message and I cannot get into the results to make any changes. The response just shows commas for missing data like  JOE,,,,TRUE 

 

  Is there a way I can define a default value or 'null' for the date in my script above? 

 

Thanks.

1 answer
0/9000

Does the following limitation of DataWeave in Apex count independently from the subscriber organization's limits in the case of an AppExchange app? 

 

> There’s a maximum of 50 DataWeave scripts per org.

2 answers
  1. Feb 24, 2025, 4:05 AM

    @Dominic Foster

    , why is there a limit?  I just discovered how useful they can be--especially translating SF-generated json (serialized) into payload for integrations. 

     

    And why would DW share Apex's heap?  It seems as though adding both those limitations (heap and scripts) are meant to discourage DW's adoption and utilization.

0/9000

During my tests I found out the heap size limit is separate for DataWeave and Apex during a single transaction, but is there a way to check the heap usage for DataWeave?

3 answers
  1. Nov 26, 2024, 10:41 PM

    Heap usage for DataWeave within an Apex transaction is counted as part of the Apex transaction's heap usage.

0/9000

I started doing some testing trying to make a request from Salesforce to a Mulesoft endpoint in  multipart/form-data. I'm wondering if there's a way to create the script in Salesforce for it,  I've created this script

 

%dw 2.0

input records application/java

output multipart/form-data

---

{

parts: {

text: {

content: "this is a content for text"

}

}

}

When calling the Apex script:

 

public static void processMultipart(){

DataWeave.Script script = new DataWeaveScriptResource.dataToMultipart();

Map<Id,Attachment> mapAttachments = new Map<Id,Attachment>([Select Id, Body from Attachment limit 10]);

DataWeave.Result result = script.execute(new Map<String, Object>{'records' => mapAttachments.values()});

System.debug('DW Result: '+ result.getValue());

}

Salesforce reports:

Is multipart/form-data supported?

Is there a sample for this, or maybe multipart/form-data is not supported yet?

 

Best!

4 answers
0/9000

Exploring an idea using CSVs generated by the Salesforce Bulk API, and the DataWeave script trips up on a rich text field. 

 

I was surprised to see that DataWeave says that the Bulk API is outputting malformed CSVs. 

 

Here's what I get: 

 

Malformed CSV input. Ignoring following chars after quoted value at offset 41215: """><a href=""https...

Here's the matching field in prod: 

<p style=""><a href="https

Does anyone here have suggestions on what I could/should do differently either with the bulk API or with DataWeave to be get past these errors? 

 

Thanks in advance!

2 answers
  1. Sep 13, 2024, 2:03 AM

    Bulk API escapes the double quotes used in HTML tags with double quote, which is proper under RFC-4180.  However I think DataWeave defaults escape to backslash, so it'd complain.  Try specify the escape char in your script, something like

    %dw 2.0

    input payload application/csv escape='"'

0/9000

I want to delete a dataweave script in a sandbox. How do I do that? thanks.

1 answer
0/9000