Hi Y'all!
First post. real problem. Have been trying for 2 days
(script below text)
I have a workplace problem that i am determined to solve using Airtable.
In table: Product data i have 1500+ product records with a primary barcode field called 'BRCDP'
In table: Scanlist i created there's a field called 'BRCDA' also a primary barcode field.
I needed a script that is be able to compare a dataset an match the records primary field, if a new records added to table: Scanlist matches a existing barcode in table: Product data, this new record should be automatically be added to the designated linked record field. It keeps crashing.
Does anyone have a simply script for this?
const productDataTable = base.getTable('Product data');
const scanListTable = base.getTable('Scanlist');
async function linkRecords() {
const queryResult = await scanListTable.selectRecordsAsync({
maxRecords: 1
});
const scanRecord = queryResult.records[0];
const scanBarcode = scanRecord.getCellValue('BRCDA');
const filterFormula = `{BRCDP} = "${scanBarcode}"`;
const productDataResult = await productDataTable.selectRecordsAsync({filterByFormula: filterFormula});
if (productDataResult.records.length > 0) {
const productDataRecord = productDataResult.records[0];
const linkedRecords = productDataRecord.getCellValue('Active Scans') || [];
const existingLinkedRecordIds = linkedRecords.map(linkedRecord => linkedRecord.id);
if (!existingLinkedRecordIds.includes(scanRecord.id)) {
linkedRecords.push({id: scanRecord.id});
await productDataTable.updateRecordAsync(productDataRecord, {'Active Scans': linkedRecords});
}
}
}
linkRecords();