Step Hooks Examples
Elisa Dinsmore avatar
Written by Elisa Dinsmore
Updated over a week ago

This article is for Flatfile's Portal 2.0. If you'd like to check out the latest version of the Flatfile Data Exchange Platform, click here!

Specific documentation for the step hooks, which can be used with Portal 2.0:

Step hooks are a powerful feature that open up a lot of options. Here’s the code that shows them in action:

import FlatfileImporter from "@flatfile/adapter";
import $ from "jquery";
import { schemas } from "./schemas";

const { contactSchema } = schemas;

const importer = new FlatfileImporter(
"LICENSE_KEY_HERE",
contactSchema
);

importer.setCustomer({
userId: "19234",
name: "Foo Bar"
});

importer.registerStepHook("upload", (stepObject) => {
console.log(stepObject);
if(
confirm(
"UPLOAD step hook. At this point, we have access to the file that was uploaded and can decide to move forward or not based on some factor like # of rows.\n\n"+
JSON.stringify(stepObject,"",2)
)
){
return true;
} else {
return false;
}
});

importer.registerStepHook("match", (stepObject) => {
console.log(stepObject);
if (
confirm(
"MATCH step hook. We get the same payload as in the UPLOAD hook but this is a good step to decide what to do based on how the headers are being auto-mapped.\n\n"+
JSON.stringify(stepObject,"",2)
)
){
return true;
} else {
return false;
}
});

importer.registerStepHook("match_field", (stepObject) => {
console.log(stepObject);
if(
confirm(
"MATCH_FIELD step hook. At this point, we can examine any changes the user makes to any field mapping.\n\n"+
JSON.stringify(stepObject,"",2)
)
){
return true;
} else {
return false;
}
});

importer.registerStepHook("review", (stepObject) => {
alert(
"REVIEW step hook.\n\nIn this hook, we can look at the data and determine how to handle it in the review. This is also the point where you can add virtual fields. In this example, a virtual field is created that combines the first and last name fields into a full-name field and then we hide the first and last name fields."
);
if (
stepObject.headers_matched.find((v)=>v.matched_key==="firstName")&&
stepObject.headers_matched.find((v)=>v.matched_key==="lastName")
) {
importer.addVirtualField(
{
label: "Full Name",
key: "full_name",
description:'Only create this field when columns "First Name" and "Last Name" are matched'
},
{
order: 2,
hideFields: ["firstName", "lastName"]
}
);
}
});

importer.registerRecordHook(async (record, index, mode) => {
let out={};
// if virtual field has been added, insert values into it
if(record.hasOwnProperty("full_name")&&!record.full_name){
out.full_name={
value: record.firstName + " " + record.lastName
};
}

return out;
});

$("#launch").click(function () {
importer
.requestDataFromUser()
.then(function(results){
importer.displaySuccess("Thanks for your data.");
$("#raw_output").text(JSON.stringify(results.data,"",2));
})
.catch(function(error){
console.info(error||"window close");
});
});
Did this answer your question?