Skip to content

Instantly share code, notes, and snippets.

@benbyford
Created August 6, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbyford/372d5781227336ce7fa106379e04feaa to your computer and use it in GitHub Desktop.
Save benbyford/372d5781227336ce7fa106379e04feaa to your computer and use it in GitHub Desktop.
Airtable copy rows to a new table when match
let table = base.getTable("References");
// get field names
const fieldName = "Name";
const field = table.getField(fieldName);
// copy to new table field options
const mustField = field.options.choices[0].id; // first tiem
const refField = field.options.choices[1].id; // second item
// get all rows and get mod ones
let queryResult = await table.selectRecordsAsync();
// new records
let copyRecords = [];
// for each row do something
queryResult.records.forEach( row =>{
let rowID;
let rowCell = row.getCellValue(fieldName);
if(rowCell) rowID = row.getCellValue(fieldName)[0].id;
if(
rowID == mustField ||
rowID == refField
){
copyRecords.push(row);
}
});
output.inspect(copyRecords);
const copyToTableName = "TableTwo";
const idFieldName = "id";
let copyToTable = base.getTable(copyToTableName);
let copyToQueryResult = await copyToTable.selectRecordsAsync();
for (let i = 0; i < copyRecords.length; i++){
copyToQueryResult.records.forEach( compareRows => {
let CompareRowID = compareRows.getCellValue(idFieldName);
if(CompareRowID == copyRecords[i].id){
copyRecords.splice(i,1);
}
});
}
copyRecords.forEach( record => {
copyToTable.createRecordAsync({
"Name" : record.name,
"Linked ID" : record.id
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment