What am i doing wrong?
const extract = await Extract.selectRecordsAsync({
Best answer by brandon698sherr
Hello @Guics ,
The issue in your script arises from how you're comparing the linked record in the extract table with the selected bank record. In Airtable, linked records are objects, so you need to compare their IDs or the specific fields directly, not as arrays.
const extract = await Extract.selectRecordsAsync({
fields: ["Name", "Data", "Valor", "Bank"]
}).then(query => query.records);
let bank = await input.recordAsync("Escolha um banco: ", Banks);
// Filter extract by the selected bank
const extractByBank = extract.filter(ext => {
let bankCell = ext.getCellValue("Bank");
// Bank cell might be null if there's no linked record
if (bankCell && bankCell.length > 0) {
return bankCell.some(linkedRecord => linkedRecord.id === bank.id);
}
return false;
});
// Log or use the filtered records as needed
console.log(extractByBank);The Extract.selectRecordsAsync() method fetches records with the specified fields from the Extract table.
The input.recordAsync("Escolha um banco: ", Banks) method prompts the user to select a record from the Banks table.
The filter method iterates over each record in the extract array.
For each record, it checks the value in the "Bank" field using getCellValue("Bank").
Since the "Bank" field is a linked record field, getCellValue("Bank") returns an array of linked records.
The some method checks if any of the linked records' IDs match the selected bank's ID.
Ensure that the "Bank" field in some records might be empty. Check for null values before accessing the array.
Ensure you are comparing the IDs directly and not using arrays for comparison.
This script should now properly filter the extract records based on the user's selected bank, returning only the records linked to that bank.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.