post map
The post map hook is invoked on a page of records after the records are mapped from source to destination structures. This hook can be used to validate, update, or ignore records before they are submitted to the destination application. Changes made to source records in this hook will persist only for the duration of the import, and will not carry over to downstream applications in your flow. This hook is a great place to execute logic on batches of records to optimize the final payload-building experience in Flow builder. For example:
- Reformat fields or object structures to avoid complex handlebars expressions
- Perform calculations on lists to avoid tedious handlebars expressions
- Load the destination record to dynamically change the fields being submitted
Function stub
/*
* postMapFunction stub:
*
* The name of the function can be changed to anything you like.
*
* The function will be passed one argument ‘options’ that has the following fields:
* ‘preMapData’ - an array of records representing the page of data before it was mapped. A record can be an object {} or array [] depending on the data source.
* ‘postMapData’ - an array of records representing the page of data after it was mapped. A record can be an object {} or array [] depending on the data source.
* '_importId' - the _importId currently running.
* '_connectionId' - the _connectionId currently running.
* '_flowId' - the _flowId currently running.
* '_integrationId' - the _integrationId currently running.
* 'settings' - all custom settings in scope for the import currently running.
* 'testMode' - Boolean flag that executes script only on test mode and preview/send actions.
*
* The function needs to return an array, and the length MUST match the options.data array length.
* Each element in the array represents the actions that should be taken on the record at that index.
* Each element in the array should have the following fields:
* 'data' - the modified/unmodified record that should be passed along for processing.
* 'errors' - used to report one or more errors for the specific record. Each error must have the following structure: {code: '', message: '', source: ‘’ }
* Returning an empty object {} for a specific record will indicate that the record should be ignored.
* Returning both 'data' and 'errors' for a specific record will indicate that the record should be processed but errors should also be logged.
* Throwing an exception will fail the entire page of records.
*/
function postMap (options) {
return options.postMapData.map((d) => {
return {
data: d
}
})
}
Example
This function will calculate and set the total field on each record in the postMapData array, after the mapping has been applied.
function calculateTotalsUsingPostMap(options) {
return options.postMapData.map(record => {
// Initialize total to 0 for each record
let total = 0;
// Check if the record has items and iterate over them
if (record.items && Array.isArray(record.items)) {
record.items.forEach(item => {
// Calculate total based on item properties, e.g., quantity and price
total += (item.quantity || 0) * (item.price || 0);
});
}
// Set the total on the record
record.total = total;
// Return the modified record
return {
data: record
};
});
}