Help

Get the current status from a single select field with airtable scripting

Topic Labels: Automations
Solved
Jump to Solution
1891 3
cancel
Showing results for 
Search instead for 
Did you mean: 
timlines
5 - Automation Enthusiast
5 - Automation Enthusiast

I'm attempting to read the current status of a single select field, what I'd like my script to do is read the status of the button and simply return that status as a string. For example, if I manually set the button to "Doing" and run the script, my script should return "Doing" from the console. 

My airable is setup with a single table called "Item Status" that lists all the records in the table. Each record has a "Current Status" field, a single select field with "To Do", "Open", and "In Progress" 

 

 

// read the current status of a record
let table = base.getTable("Item Status");
let queryResult = await table.selectRecordsAsync({fields: ["Current Status"]});

console.log(queryResult);

let record = queryResult.records[0];
console.log(record.getCellValue("Current Status"));

 

 

 The problem is when I run this script it returns more data then I want. 

 

 

CONSOLE.LOG
{id: "selkMEZY58GC8AUfc", name: "Open", color: "blueLight2"}
id: "selkMEZY58GC8AUfc"
name: "Open"
color: "blueLight2"

 

 

 I don't need to know the id, or color, I'd prefer for now to only return the string "Open" if the button is set to open. I understand that each type of cell has a typeDef  but I don't understand how to read from this typedef

 

 

TYPEDEF
{
    choices: Array<{
        id: string,
        name: string,
        color?: string,
    }>,
}

 

 

This is part of a simple project where I'm trying to record the status of each record and keep a log of when it was updated in order to track how long each record is in each status. 

timlines_0-1679599925848.png

 

 

1 Solution

Accepted Solutions
Ben_Young1
11 - Venus
11 - Venus

 

 

const table = base.getTable("Item Status");
const records = await table.selectRecordsAsync({
    fields: ["Current Status"]
})
    .then(query => query.records);

let record = records[0];

console.log(record.getCellValueAsString("Current Status"));
console.log(record.getCellValue("Current Status")?.name);

 

Single select fields return a choice object containing the data of the current single select value (assuming one is present.)

 

{
    id: string,
    name: string,
    color?: string,
}

 

Each single select choice has three properties: the unique ID of that particular option, the string value that is displayed in the actual field, and an optional color property if you have colored options enabled. 

To get the string value, you can use the record.getCellValueAsString() method.
You can similarly use the record.getCellValue() method, but you will need to access the name property.

Here's an example:

 

/*
In a single select field, we have the following option selected: "Choice 1".
(Option coloring is set to off.)
*/

let singleSelectFieldValue = record.getCellValue("yourField");

/*
The value of singleSelectFieldValue is:
    {
        id: "idString",
        name: "Choice 1"
    }
*/

let valueAsString = record.getCellValueAsString("yourField");
// The value of valueAsString is: "Choice 1".

 

See Solution in Thread

3 Replies 3
Ben_Young1
11 - Venus
11 - Venus

 

 

const table = base.getTable("Item Status");
const records = await table.selectRecordsAsync({
    fields: ["Current Status"]
})
    .then(query => query.records);

let record = records[0];

console.log(record.getCellValueAsString("Current Status"));
console.log(record.getCellValue("Current Status")?.name);

 

Single select fields return a choice object containing the data of the current single select value (assuming one is present.)

 

{
    id: string,
    name: string,
    color?: string,
}

 

Each single select choice has three properties: the unique ID of that particular option, the string value that is displayed in the actual field, and an optional color property if you have colored options enabled. 

To get the string value, you can use the record.getCellValueAsString() method.
You can similarly use the record.getCellValue() method, but you will need to access the name property.

Here's an example:

 

/*
In a single select field, we have the following option selected: "Choice 1".
(Option coloring is set to off.)
*/

let singleSelectFieldValue = record.getCellValue("yourField");

/*
The value of singleSelectFieldValue is:
    {
        id: "idString",
        name: "Choice 1"
    }
*/

let valueAsString = record.getCellValueAsString("yourField");
// The value of valueAsString is: "Choice 1".

 

kanyeshirt6
4 - Data Explorer
4 - Data Explorer

Each week, runners in the Run group I manage log in to Airtable using a form entry from a "Check-Ins" table. This form retrieves information (names) from a more comprehensive "Runner List" that records the login histories of all registered runners. The "Runner List" master sheet, which totals the number of runs each individual has attended, is synced back to the "Check-Ins" table, which keeps account of the weekly login for each run. I want to build a new table that displays the number of new runners that appear each week.

timlines
5 - Automation Enthusiast
5 - Automation Enthusiast

Thanks Ben, I'm not sure if this is the right answer but I think this might be one step closer to a solution.