Help

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

6264 0
cancel
Showing results for 
Search instead for 
Did you mean: 

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:
image

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

14 Replies 14

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

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.

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

EKBrang
4 - Data Explorer
4 - Data Explorer

@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);
Chuck_F
5 - Automation Enthusiast
5 - Automation Enthusiast

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.

Hi, @Chuck_F. Any chance you figured this out?

I’m trying to create a new record, and if the new records “industry” tag is not included in the current selection options, I’d like to add it. I’m trying the following right now with no success:

// Companies is table for which I'm trying to add a new record 
// AND introduce a new Industry
let tblCompanies = base.getTable('Companies');

// Industries is a multi-select field
let fldIndustries = tblCompanies.getField('Industries');

// As a first step, I'm trying to push the new industry into the select field
// And this is the line that isn't working
fldIndustries.options.choices.push ({name: 'test industry'}]);

Any help (from anyone) would be very much appreciated.

Thanks.

As noted upthread by Kasra, it is not possible to create a new option for a single-select or multi-select in a script.

When I have situations like this where it is impossible to know all the possible options in advance, I change the field type to a text field.

I believe that adding options can be done in a custom block, but custom block development is far more work than creating a script.