The DataMapper package provides two Flow Actions that can be combined in different ways to achieve powerful transformations between Salesforce objects and JSON. We’ll demonstrate this using the JSON created in the example in the Make HTTP Call. In that example, a call is made to a foreign exchange web service and it returns the exchange rates for ALL the traded currencies. But we only need a single exchange rate.
Extract JSON Data
Here’s what the JSON returned by the call looks like in a test callout:
Let’s look at it with a little bit of formatting:
In our example, we want to extract the exchange rate of the US Dollar against the Australian Dollar. We can see that at the moment the above call was made, that rate was 1.2945. Let’s extract that with Extract JSON Data, the first of the two Datamapper actions:
As you can see above, we pass the Body JSON in from the HTTP response and then reference the AUD rate by giving the dot-notation path to it. When you use this action, you provide keys and assign their values to 1 of 20 available outputs. These static outputs are labeled ‘value1’, ‘value2’, etc… and they’re strings. You can use those values downstream in the flow or you can assign them to more memorable variables.
This test output shows the successful extractions:
Map JSON Array: Handling groups of JSON objects
Extract JSON Data is powerful but it has a flaw: you can’t practically use it against arrays. And arrays show up all over the place. Here’s an example from a Slack API that can return 1 or more attachments:
The most practical way to manipulate arrays of JSON data is to map them to a corresponding collection of Salesforce SObjects. That’s the function of the second DataMapper action: Map JSON Array. When you configure this action, you first need to isolate the array JSON that you want to map. Here’s what an isolated array json string looks like:
In our example, we’ll use this json data to create records of type ‘CustomAttachment__c’:
Here’s what the Map JSON Array looks like:
This action will output a collection of the selected Target Object Type, which you can use to create a new set of records or do futher processing:
The keys on the right will be looked for in the individual json array members, and their respective values will be set to the specified output fields.
- Developer Note: The underlying action uses a generic SObject output. The CPE makes sure that a concrete type (in this case, CustomAttachment__c) is provided.
- Developer Note: The variable List Builder mechanism used here has been implemented so that it can be reused easily in unrelated CPE’s.
This action doesn’t currently support the ability to edit/update existing records, but that could be added in the future.
Here’s a view of the resulting Custom Attachment records created using the record collection variable output by the Map JSON Array:
Version 126.96.36.199 Unmanaged 6/19/21 JSON can be mapped to Date fields if it is in the form of a Date.
Version 188.8.131.52 Unmanaged 6/13/21