Help

Issue with airtable script: Find records matching a value from a single/multiple select field

Topic Labels: Scripting
Solved
Jump to Solution
1662 1
cancel
Showing results for 
Search instead for 
Did you mean: 
kofal
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi All,

I am having an issue with my script:

When using an integer value in the condition, the script works.

Example: return all records that have a value higher than 9 in the field name "Bedroom":

 

const propertyTable = base.getTable("Listing");

// Read the data from my Listing table
const recordsFromproPertyTable = await propertyTable.selectRecordsAsync (); 

//Filter records matching a condition
const matches = recordsFromproPertyTable.records.filter((records)=>{
  return recordsFromproPertyTable.records.find((potentialmatch)=> {
       return records.getCellValue("Bedroom") > 9;
        }
    )
})

console.log(matches)

 

When trying to match the field value with a single select or multiple select field, I am getting the following error:

This condition will always return 'true' since the types  and 'string' have no overlap."

I am pretty sure this is due to the fact that I am comparing an Array with text but I am not sure how to solve this:

Return all records that have a value of "Villa" for the field name "Property type" (Single select field type)

 

const propertyTable = base.getTable("Listing");

// Read the data from my Listing table
const recordsFromproPertyTable = await propertyTable.selectRecordsAsync (); 

//Filter records matching a condition
const matches = recordsFromproPertyTable.records.filter((records)=>{
  return recordsFromproPertyTable.records.find((potentialmatch)=> {
       return records.getCellValue("Property type") === "Villa";
        }
    )
})

console.log(matches)

 

  Any help would be immensely appreciated.

1 Solution

Accepted Solutions
kofal
5 - Automation Enthusiast
5 - Automation Enthusiast

50 youtube videos later, I did some modification and could kinda make it work.

 

// Script setup

let opportunitiesTable = base.getTable("Opportunities");

// Ask the user for Input

let opportunity = await input.recordAsync("Select opportunity", opportunitiesTable);

console.log(opportunity)
// Get the listing table

let propertiesTable = base.getTable("Listing");

let allProperties = await propertiesTable.selectRecordsAsync({fields:["Property Name","Budget range","Property type","Land type", "View type"]});

// Filter all properties that could match

let properties = allProperties.records.filter(property => {
    // @ts-ignore
    return property.getCellValueAsString("View type") === opportunity.getCellValueAsString("View type")
});

console.log(properties)

The code below works great with a single select field since it passes the field value as text and compares them with each other. 

I am hitting a limitation with multiple select field types tho. The current script only filters records where the targeted field is an exact match. Is there a way to change it so that it acts like the usual airtable filter "Has any of the options"

See Solution in Thread

1 Reply 1
kofal
5 - Automation Enthusiast
5 - Automation Enthusiast

50 youtube videos later, I did some modification and could kinda make it work.

 

// Script setup

let opportunitiesTable = base.getTable("Opportunities");

// Ask the user for Input

let opportunity = await input.recordAsync("Select opportunity", opportunitiesTable);

console.log(opportunity)
// Get the listing table

let propertiesTable = base.getTable("Listing");

let allProperties = await propertiesTable.selectRecordsAsync({fields:["Property Name","Budget range","Property type","Land type", "View type"]});

// Filter all properties that could match

let properties = allProperties.records.filter(property => {
    // @ts-ignore
    return property.getCellValueAsString("View type") === opportunity.getCellValueAsString("View type")
});

console.log(properties)

The code below works great with a single select field since it passes the field value as text and compares them with each other. 

I am hitting a limitation with multiple select field types tho. The current script only filters records where the targeted field is an exact match. Is there a way to change it so that it acts like the usual airtable filter "Has any of the options"