Help

GET API for Search by keyword

1888 4
cancel
Showing results for 
Search instead for 
Did you mean: 
Prashanth_Kumar
5 - Automation Enthusiast
5 - Automation Enthusiast

I have a simple table called “Imported table” where Field 1 is merchant category and Field 2, 3, 4…10 are merchant names.

I am looking for a JS function with merchant name as parameter and if present in the database, can return the corresponding Field 1 name.

One option that I’ve been trying is filterbyFormula as REGEX_Match and loop from field 1 to 10, but can’t seem to figure how to complete the function. Appreciate any inputs on this or better ways to do this.

4 Replies 4
Prashanth_Kumar
5 - Automation Enthusiast
5 - Automation Enthusiast

My code is as follows, but line 4 seems to throw an error. Any suggestions on how to use the filterbyFormula?

function checkmerchant(merchant) {
let table = base.getTable(“Imported table”);
for (let field of table.fields) {
let recordFound = table.select({
filterbyFormula: REGEX_MATCH({${field.name}},"${merchant}")
})
console.log(recordFound[0]);
}
}

checkmerchant(‘MAKEMYTRIP’);

The formula that you’re using assumes that all of the queried fields will return strings (REGEX_MATCH() requires a string as its first argument). If any of those fields returns an array, that could be the cause of the error. What are the field types that you’re querying?

@Justin_Barrett thanks for the response!

I was trying this code on Airtable Scripting and I think I read somewhere that Scripting doesn’t support filterbyformula. However, I was able to run this on node js with a few minor changes to the code as below.

Separately, I am looking for the tool to just output the matched keyword and not the full record? What change to records[0] would I need to implement? thank you

function checkAirtableId(merchant) {
let tabletest = base(‘Imported table’);
for (let i = 1; i < 15; i++) {
let y = ‘Field’ + ’ ’ +i;
let recordFound = base(‘Imported table’).select({
filterByFormula: REGEX_MATCH({${y}},"${merchant}")
}).firstPage(function(err,records) {
if(err) {
console.log(err);
return;
}
console.log(records[0]);
})
}}

Correct. The API for Airtable’s Scripting app is not the same as the API used when querying Airtable from an outside server. They use completely different ways of retrieving and interacting with the data. The proper way to filter records inside of Airtable’s scripts is to collect the records first using selectRecordsAsync, then filter the results using JavaScript’s normal .filter method. The Scripting app API docs are available at the bottom of the Scripting app interface.

Regarding your second question, it’s not just a matter of changing records[0]. There are numerous things in your script design that don’t follow the proper methods of accessing Airtable data from within the Scripting app. The app’s API is documented at the bottom of the app interface. I suggest taking some time to read through it and familiarize yourself with the differences, then rewrite your script.