Create Single-Select / Multi-Select Field Options from Script

I’m guessing the answer is no, but is it possible to add a new option to a Single-Select or Multi-Select field programmatically from a script?

At first I tried to just brute force it - I tried to enter a string value for the Single-Select field via table.updateRecordAsync(). This is a way we can add new options via the main UI, although the UI presents a context menu for doing so.

Next, I tried to see if I could push() a new value into

table.getField("Single-Select Field").options.choices

but the API returns the options as immutable, since it’s a get[***]() function:

Any way to do this, or any plans to support it in the future?

4 Likes

There isn’t a way to do this with the Scripting API right now, but noting your +1 for this improvement!

3 Likes

Thank you. Ideally it could work the same way as the UI – just update or create a record, filling the Single-Select or Multi-Select field with a string value. If the string value doesn’t exist as an option, Airtable would create it as an option, choosing a random color to assign it.

2 Likes

I’ve experienced this limitation in Integromat integrations where I cannot push new or updated records to Airtable if a field value is not already in the field’s options list. I think this issue will need to be addressed from all fronts.

This also matches the behavior of the standard API (the one documented at https://airtable.com/api that existed before the Scripting API). Are there clear names to differentiate between these two API’s?

Hi @Kasra - in a similar vein, would be great if we could create a linked record by name, rather than just by id. In the UI, we can copy a value into a linked record field and it makes the link if the string value already exists as the primary field or creates a new record in the linked table with the value if it doesn’t.

I was trying to script this behaviour, but you can currently only add a link if you know the id and if the id doesn’t exist…I know you can go to the linked table, create the new records, then go back and add the link, but this feels a bit long-winded.

JB

@Jeremy_Oglesby, on a semi-related note. I was using your “Expense Tracking” script (which is awesome) as a basis for auto-populating single select fields in my base. Do you know if it is possible to set the color of the single select option? And have you modified your script to work with multiple select tables? I’m in the process of trying to see how that might be done (or if it is even possible in airtable). Currently it sees the list thing1, thing2, thing3 as one item and therefore can’t match it to selections that are broken out individually. I tried using split(", ") but I think I’m lacking the modification elsewhere for this to work as intended

// Loop over each transaction...
WriteTo.forEach(transaction => {
let DataField = transaction.getCellValue(BaseSpecificNames.WriteToFieldName);

// If this transaction already has the Payee name match made, find the name there; if not, this is blank
let DataName = DataField ? DataField.name : '';

// If the payee Single Select field is empty (this transaction has not been matched yet)
// then check for import name matches in the matches table
if (DataName === '') {
// matchFound defaults to false until/unless a match is found
let matchFound = false;
let transactionPayee = transaction.getCellValue(BaseSpecificNames.MatchWriteToFieldName);
if (transactionPayee !== null){
let list= transactionPayee.split(", ");
// Loop over each record in the matches table to check them against the import name of the transaction
DataToCopy.forEach(DataToCopy => {
// If a match is found between import name and an existing match record, set matchFound to true
// and add the transaction record to the list of records to be updated with the payee name found
if (list === DataToCopy.getCellValue(BaseSpecificNames.CopyFromFieldName)) {
matchFound = true;
let matchPayeeField = DataToCopy.getCellValue(BaseSpecificNames.CopyFromFieldName);
let matchPayee = matchPayeeField ? matchPayeeField.name : '';

let newValue = WriteToFieldOptions.find(o => o.name === matchPayee);

// Field name must be manually changed here since only a string value can be supplied. Field name is same as WriteToFieldName

let record = {id: transaction.id, fields: {"Process Area": newValue}};

transactionsToUpdate.records.push(record);

I would also like to be able to add a single select option via script. It would be easy enough to code on our end and would make a lot of sense. Not being able to do so interrupts the workflow benefits of the scripting block and adds confusion to the end user, which is what I primarily try to eliminate with the scripting block.