Help

How to check multiselect cell value and compare it to formula result cell value to perform an action if the values are the same?

Topic Labels: Scripting extentions
1754 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Matija_Spiljar
4 - Data Explorer
4 - Data Explorer

Hello Airtable community,
I am new to Airtable and still learning javascript, reading trough this community has been very helpful so far.
However, I can not find a solution to what seems to be an easy task. Please help.
Working with 3 tables:

  1. dayTable

  2. shiftTable

  3. scheduleTable

In dayTable records are automatically created based on the script and automation which I created (the user picks the week start date, then the formula calculates 7 days and finds all dates in between, and then creates a record for every day in another table = dayTable, in dayTable weekday formula is showing me what day it is

In shiftTable shifts are created, some shifts are every day, and other occasional which is determined with single select field. For occasional shifts there is a multiple select with 7 options (1 for each day)

In scheduleTable there is 1 record for each day+shift, (for example there are 2 everyday shifts, and in scheduleTable Moday has 2 records - Monday morning and Monday evening, and those records are created automatically using scripts and automation, this is where I got so far)

I would like to include a script that checks a dayTable for a weekday cell value and shiftTable for multiple select values and if multiple select contain a value that is the same as the weekday value (for example Friday) then a new record should be created in the shifts table.
Seems to me like I would have to get value as a string and compare it to a value of a weekday but can not get it right. Multiple select is an array of objects, how to access single-cell values? I am trying to find a solution like record.getValueAsString(‘multiple select’).name but can not figure it out. As I said still learning javascript, please help!

Thank you in advance :grinning:

1 Reply 1

Welcome to the community, @Matija_Spiljar! :grinning_face_with_big_eyes:

With the array of objects that comes back from a multiple-select field, each object has a name property that is the displayed name of the item. First you can iterate over those objects to isolate the names by using the map() array method:

const names = record.getCellValue("multiple select").map(item => item.name)

As a side note, it’s also a good idea to handle cases where the multiple-select field might be empty. Here’s an extension of the above line that does this:

const names = (record.getCellValue("multiple select") || []).map(item => item.name)

This part here:

record.getCellValue("multiple select") || []

effectively says that if what comes back from the multiple-select field is falsy—which it will be if Airtable returns undefined from an empty field—then use an empty array as a fallback value. That way the .map() method will have an array to work with no matter what: either an array of objects, or an empty array.

Now you can look to see if that array of names includes the weekday name from the other field:

const weekday = record.getCellValue("Weekday")
if (names.includes(weekday)) {
  console.log("We have a match!")
} else {
  console.log("No match found.")
}