Pre Map hook in Celigo

Pre map

The premap hook is invoked on a page of records before the records are mapped from source to destination structures. This hook can be used to validate, update, or ignore records before mapping rules are run. Changes made to records in this hook are localized and will not get passed along to subsequent steps in the flow. This hook is a great place to execute logic on batches of records to optimize visual field mapping. For example:

  • Reformat fields or object structures to avoid complex mappings
  • Perform calculations on lists to avoid tedious mapping expressions
  • Load the destination record to pre-populate data needed by mapping logic

Function stub

/*
* preMapFunction stub:
*
* The name of the function can be changed to anything you like.
*
* The function will be passed one ‘options’ argument that has the following fields:
*   ‘data’ - an array of records representing the page of data before it has been 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 preMap (options) {
  return options.data.map((d) => {
    return {
      data: d
    }
  })
}

Example

This function will calculate and set the total field on each record in the data array, before the mapping has been applied.

function calculateTotalsUsingPreMap(options) {
  return options.data.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
    };
  });
}

Leave a comment

Your email address will not be published. Required fields are marked *