Created
August 6, 2020 13:03
-
-
Save benbyford/372d5781227336ce7fa106379e04feaa to your computer and use it in GitHub Desktop.
Airtable copy rows to a new table when match
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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