Use the "search" function in the "filter by formula" of Airtable plugin with a lookup record

Hi,

I have two tables: Clients and Employees. Employees is linked to Clients and a column “client_id” displays the related airtable client id.
I would like to use the filter by formula in the Airtable plugin to find all records in the Employees table that have the related airtable id.

What can I use ?

2 Likes

Hi @MaxenceP :wave:

Here are the docs on the subject: Airtable data source | WeWeb documentation

The tricky part is getting the quotation marks right but the use case presented in the article seems to match very closely to what you’re trying to do

1 Like

Hi Joyce and thank you for your answer.

I’ve already used the search function of this doc for a more basic approach: a simple column in a table and it works great.
Here it doesn’t work, probably because the column I’m doing my search is an array.
The forumula I tried is this one 'SEARCH("'+weweb_variable_for_entreprise_id+'",entreprise_id)' where entreprise_id is the lookup field of my clients table.

Alright!

It took me a while. Here’s the whole process: Maxence - filterByFormula with list in Airtable — Tella

Want to skip ahead?

  • use the ARRAYJOIN formula in Airtable to turn your array into a string
  • this will allow you to use Airtable’s FIND or SEARCH formulas (which can only search through strings) when setting up your collection in WeWeb

Thanks for the inspiration @MaxenceP! Even though it’s an Airtable limitation, it’s a great topic for a future WeWeb tutorial.

Wow great work, you saved me a lot of time here…
Thanks a lot

1 Like

You can also use conditionals along with the RECORD_ID()=client_ID. Just string all the client_ID’s that you want to match together. If you want to find an Employee who has all the clients you should use AND, if you would like to find an employee who has any of the clients use OR.

I think this is a bit simpler than using the search function.

The syntax would just be:

OR (
RECORD_ID()=client_ID,
RECORD_ID()=client_ID,
RECORD_ID()=client_ID
)

You would create this OR / AND with the help of a little javascript although I’m sure you would also be able to do it with the loop feature in the workflow.

// Array of record IDs a weweb formula inserted into your javascript editor
var recordIds = variables[‘9c13e8ec-bbaf-414b-9e8e-d85bb6df20f3’];

// Initialize an empty string to store the OR formula
var orFormula = ‘’;

// Iterate through the recordIds array to construct the OR formula
for (var i = 0; i < recordIds.length; i++) {
// Add the condition for the current record ID to the OR formula
orFormula += “RECORD_ID() = '” + recordIds[i] + “'”;
// Add “,” if it’s not the last element
if (i < recordIds.length - 1) {
orFormula += ", ";
}
}

// Construct the final formula with the OR function
var finalFormula = “OR(” + orFormula + “)”;
return finalFormula

This entire dynamically created formula could go into your filter for your collection.

2 Likes

Hi @benFortunato and thank you for this interesting way of doing stuff.

While @Joyce answer as enough for me at the time, it no longer is, and maybe you can help:

→ The SEARCH behaves as a CONTAINS which is a problem for me.

Would you know a way to make this formula more robust so that it behaves as a IS and not a contains ?

if not I’ll create a post on the topic :wink:

As a reminder, the formula right now looks like this ('SEARCH("'+variable containing user id+'",id_airtable)'

Instead of search just use a simple equality statement, Record_ID()=‘SearchID’ or you could just check for any other value {airtableFieldName}=‘searchTxt’. If you need to search for multiple values then use OR and/or AND depending on what kind of functionality you want.

That is what I did in the script I wrote so I can input an array of search terms and then create an OR function that includes all the terms within that array. You could motify the script to add a SEARCH formula so that you could search for any record that contains one of the search values.

Essentially the filter formula in the API is looking for a boolean value. Your formula should generate a true false value or 0,1. Anything that generates that result will work.

If you are just looking for a simple search of a field the formula is “{yourAirtableFieldName}=‘IdToMatch’” Make sure that the idtomatch is in quotes. If you want to use multiple fields just use “OR({yourAirtableFieldName}=‘IdToMatch’,{yourAirtableFieldName2}=‘IdToMatch2’)”

The way i set it up was to create a seperate function that generates the airtable search field text with the field that you want to match, and the array of ID values as input parameter. That way you can resuse this in any workflow that you might have. You could add anouther parameter to change from AND to OR if you need that.

Ok got it thank you ! had to work around a bit so I can have my id to look for as a variable but it works now.
You rock !