Feb 23, 2020 02:22 AM
Is there a way to retrieve specific records without iterating through all the results of a table using
table.selectRecordsAsync() or view.selectRecordsAsync()? Something like SELECT / WHERE queries in SQL? Or in AirTable terms, a Filter? This would be far more efficient than having to iterate through returned records checking for the records I’m interested in…
Jun 24, 2020 01:58 AM
I did a quick write up of Javascript filter
here:
Sep 06, 2020 09:38 AM
This appears to only work on the first field in a table which is the index. This doesn’t work for other fields in the table?
Sep 07, 2020 01:40 AM
Hi @Lisa_Hagan - yes the script is specifically pointing to the email field in my example:
let filteredRecords = query.records.filter(person => {
return person.getCellValue('Email').includes(searchTerm)
})
But this could be changed to match against any field in the table
Sep 07, 2020 06:35 AM
HI @JonathanBowen, any field could be searched on but the return value will always be for the first field.
Sep 07, 2020 12:23 PM
H @Lisa_Hagan - what you get back is an array of the filtered records. By default this contains the id and the name (the Airtable record ID and the primary field value:
But you can loop through this array and access any of the fields attached to each record. So if I have this:
Then I can access the first name like this:
for (let person of filteredRecords) {
console.log(person.getCellValue('First Name'))
}
So, the full script is:
let table = base.getTable('People');
let query = await table.selectRecordsAsync();
let searchTerm = await input.textAsync('Enter your search term');
let filteredRecords = query.records.filter(person => {
return person.getCellValue('Email').includes(searchTerm)
})
console.log(filteredRecords);
for (let person of filteredRecords) {
console.log(person.getCellValue('First Name'))
}
Sep 07, 2020 12:24 PM
Or more generally:
for (let person of filteredRecords) {
console.log(person.getCellValue('YOUR FIELD NAME'))
}
Sep 08, 2020 07:45 AM
Thank you Jonathan. This makes sense.