Help

Re: How to match / write into a single-select field?

Solved
Jump to Solution
6650 0
cancel
Showing results for 
Search instead for 
Did you mean: 
aalj
4 - Data Explorer
4 - Data Explorer

Hi - I have a newbie question here. I am writing a script with the script block to write a value into a single select field but I keep bashing my head against the wall. Can someone give me some guidance?

Here’s the short and skinny:

If I use the below, it all works, because the “ZIP CODE” field is of type “single line text”. I can write “12345” into it all day long.

 let newPreference = "12345";
// update the notes history value and reset the notes value
VoterTable.updateRecordAsync(record, {
    "ZIP CODE": newPreference
}) 

However, in the below, the “PARTY” field is of type “single-select”. One of the options is “Green” but I cannot write “Green” into the field at all. In fact, I cannot write ANY of the options into the field at all. I tried prepending the ordinal value as “11Green” and “11|Green” (Green is the 11th value) and nothing works. Every time I run this, nothing happens.

 let newPreference = "Green";
// update the notes history value and reset the notes value
VoterTable.updateRecordAsync(record, {
    "PARTY": newPreference
})

Any help for a newbie is appreciated … thanks.

1 Solution

Accepted Solutions
Justin_Barrett
18 - Pluto
18 - Pluto

Welcome to the community, @aalj! :grinning_face_with_big_eyes: The syntax for updating a single-select field isn’t terribly intuitive. There are actually multiple ways to do it, but here’s how to pull it off using the text of the item you want to select:

let newPreference = "Green";
// update the notes history value and reset the notes value
VoterTable.updateRecordAsync(record, {
    "PARTY": {name: newPreference}
})

See Solution in Thread

6 Replies 6
Justin_Barrett
18 - Pluto
18 - Pluto

Welcome to the community, @aalj! :grinning_face_with_big_eyes: The syntax for updating a single-select field isn’t terribly intuitive. There are actually multiple ways to do it, but here’s how to pull it off using the text of the item you want to select:

let newPreference = "Green";
// update the notes history value and reset the notes value
VoterTable.updateRecordAsync(record, {
    "PARTY": {name: newPreference}
})

Thank you that works! You are right, the documentation for that syntax is pretty much non-existent, at least with the first 5 pages of Goog search that I did. :slightly_smiling_face:

Google definitely won’t help you. You need to know what’s expected by the scripting block API, and that’s actually documented in the API itself, at the bottom of the editor. I actually found the hint in the popup annotation while testing the code, but if you look in the “Cell values & field options” section of the API docs, you’ll find it there as well.

If you would, please mark my answer above as the solution to your question. This helps others who may be searching with similar questions. Thanks!

sam-lateef
4 - Data Explorer
4 - Data Explorer

Hi @Justin_Barrett , i'm having same problem, trying to update single select field option from an automation script, used above code and still getting "Error: Field "fldoa21VwQ6w9iKss" cannot accept the provided value.

not sure what I'm missing!

code:
const table = base.getTable('Assets');
// Set up input variables
let inputConfig = input.config();
let Category1 = inputConfig.Category1;
let recordID = inputConfig.recordID;
let newOption = "NewOption";

if(Category1.toString() == "Exhibit")
{  

table.updateRecordAsync(recordID, {
    "Category2": {name: newOption}
});
thanks for help
}

Hi Sam,

While not directly related to the issue you've hit, you should add the "await" keyword at the start of the line where you're attempting to update the record. That tells the interpreter to wait for the update to complete before proceeding. When using any Airtable method (a function tied to an object) that ends in "Async", the "await" keyword should be present.

await table.updateRecordAsync(recordID, {
    "Category2": {name: newOption}
});

That aside, it's tough to accurately assess the situation by seeing the script alone. Here are some things to check on the field itself:

  • Confirm that the field is actually a single-select field (double-click the field header to edit the field options and confirm the field type there). Some get single- and multiple-select fields confused, and they have a multiple-select field when they think it's a single-select.
  • If it's actually a single-select field, confirm that it already has an option named "NewOption". Spelling, capitalization, and whitespace (i.e. the presence or absence of spaces) are all important. The string you're using in your code must be an exact match for an option set on the field or Airtable will complain. A common hiccup is an errant space after the last character in the name. This won't be easy to spot unless you select the name and look for a gap after the last character.

Another way of testing the situation is to run a simple script in a Scripting extension. This script would target a single record and try to perform the same type of update that you're trying in your automation script. The bonus with this test is that the Scripting extension provides much more detailed feedback when there's an error during script execution, whether it be an invalid option, a field type mismatch, etc. It will even provide some interactive feedback while editing by underlining part of the code with a wavy red line if something is wrong. If you see that, hover the mouse over the line to see the problem.

Here's a quick script that you can use to try this test. Replace the contents of the "recordId" string with an actual record ID from the Assets table in your base.

const table = base.getTable("Assets")
const recordId = "INSERT_ID_HERE"
const record = await table.selectRecordAsync(recordId)
if (record) {
    await table.updateRecordAsync(recordId, {
        "Category2": {name: "NewOption"}
    })
}

I'm hoping that some of the tips above will help you pinpoint the problem. If not, let us know and we can dig more deeply.

-Sam
5 - Automation Enthusiast
5 - Automation Enthusiast

That works! thank you so much Justin! it was a white space,  you not only solved this problem, but you also gave me insight into how to debug, I'm still new to airtable but coming from JS and React I see the potential here. with a community that has helpful experts like you, the sky is the limit. Thank you again.