May 20, 2024 01:13 PM
Solved! Go to Solution.
May 21, 2024 12:06 AM
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.
May 21, 2024 12:06 AM
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.
May 21, 2024 08:20 PM
you can also use built-in filter of selectRecordsAsync, which can be set by recordIDs parameter.
I suppose that your Extract table linked to banks in Banks table. That means each bank has respective links to Extract table. The link is array of objects like { name : 'City Bank' , id : 'recXyZ1234567890' }
at first, you ask user to select record in Bank table. then take cellValue of the field, linked to Extract (you should insert the real name of your field instead of 'Link to Extract')
then using map, you extract only id from each object. The result is array of IDs.
it can be done like .map(obj=>obj.id) . But I would prefer more descriptive way:
let bank = await input.recordAsync("Escolha um banco: ", Banks).
then(rec=>rec?.getCellValue('Link to Extract').map(({id,name})=>id))
const extract = await Extract.selectRecordsAsync({
fields: ["Name", "Data", "Valor", "Bank"],
recordIds : bank
}).then(query => query.records)