preMap Hook in Celigo

The pre map 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 the mapping experience in the UI. 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

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 *