FAQ: How do I define alternate logic for records that contain an empty field?

You can use a preMap hook to build alternate logic for a specific field in records with absent values for that field. If you are exporting records that contain a field that is occasionally empty and want to use alternate processing logic to populate a value for that field (when empty) on import, you can use the following preMap hook script strategy:

function preMap (options) {
  return options.data.map((d) => {
    if (!d.fieldThatIsSometimesEmpty) {
      d.fieldThatIsSometimesEmpty = 'Not empty anymore!'
    }
    return {
      data: d
    }
  })
}

This script checks to see if a field named "fieldThatIsSometimesEmpty" has no value populated for the record. If "fieldThatIsSometimesEmpty" does not have a value, it populates the field with the string: "Not empty anymore!".

To use this example script, replace the field name "fieldThatIsSometimesEmpty" with the field name from your record structure, and replace the string "Not empty anymore!" with the value to insert when the record returns an empty value for the field.

For more information on writing hook scripts, see Hooks for integrator.io.

0

Comments

2 comments
Date Votes
  • Hi @...,

    I've revised the post to be a more general example and included instructions for how to modify the script to work for most use cases. The original post was written for specific record structures. Please try again using the revised instructions, and let me know if you still run into problems.

    Tom

    1
  • I tried it with the following data:

    {
      "record": {
        "id": 5347,
        "company": "",
        "first_name": "Sample",
        "last_name": "Person",
        "email": "sample.person@gmail.com",
        "phone": "3056661212",
        "form_fields": null,
        "date_created": "Fri, 21 Jan 2022 01:26:16 +0000"
        }
    }

    I wanted to put in a replacement value for when the form_fields, like you see in this record, are null.  When it is populated, it is a subarray with three known values. I tried the code above, and I could NOT get it to work.  I'd really LIKE to get something in place of that data if that field is null.  Is there another way?

    0

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post