Jun 29, 2022 12:19 AM
Hi all,
so I wanted to check if there is way to retrieve a specific record based on filter criteria,
Based on my Understanding so far i can only use table.selectRecordsAsync() to get the array of all the records . later need to perform filter using linear search on the returned array.
i wanted to check if i can get a specific record based on query condition as a response.
for example : From table animals … i need record where name == X, is there a better way than getting all the records and then i need to loop through until i find X.
kindly help me with this.
thanks,
Harshith
Jun 29, 2022 07:02 AM
Unfortunately there isn’t a way to filter the records as part of the selectRecordsAsync()
query. Looping through the query result to find a specific record is very quick, though. Even with hundreds of records, it’s not going to add a noticeable delay to the script. If you use the .find()
array method, it’s very easy:
const table = base.getTable("Table Name")
const query = await table.selectRecordsAsync({fields: ["Name"]})
const match = query.records.find(rec => rec.getCellValue("Name") === "Name To Find")
if (match) {
// Code to run if a match is found
} else {
output.text("No match found.")
}
As a side note, if {Name}
is the name of your primary field, then the find line could be changed to:
const match = query.records.find(rec => rec.name === "Name To Find")
Jun 29, 2022 07:11 AM
If all you know is the record name, you must filter after selecting, as Justin pointed out.
On the other hand, if you have access to the record ID through a linked record field, you can get that record by ID using selectRecordAsync
.