Help

Create multiple select option if doesn't exist

Topic Labels: Scripting extentions
6686 10
cancel
Showing results for 
Search instead for 
Did you mean: 
Tim_Arnold
4 - Data Explorer
4 - Data Explorer

Is there a way to create a multiple select options via scripting blocks? I would have though if you set a field to a new option that doesn’t exist it would create it but doesn’t seem to be the case

10 Replies 10

Welcome to the community, @Tim_Arnold! :grinning_face_with_big_eyes: I’m not aware of a way to do what you want. The scripting block lets you do a lot, but nothing in the way of editing the actual setup of a field, such as adding options to a single- or multiple-select field. In a way, it’s kind of like the scripting block has permissions equivalent to an editor-level collaborator who can modify the data using the existing field options, but is unable to customize fields to change those options.

bummer, it would be awesome if it could create them or at least throw an error if it doesn’t exist

Yes, it would be nice!

This solution doesn’t use the scripting block, but Zapier does have the ability to add new options into the multiple-select dropdown if the newly-entered value doesn’t already exist. It’s just built into the way Zapier functions (it must be operating at the creator level) — if you add a value to a single-select or multi-select field that doesn’t already exist, it will get added into the dropdown values.

(Although it would be really nice if Airtable enabled editors to edit the dropdown menu options — that’s often a big part of data entry.)

ROARK
6 - Interface Innovator
6 - Interface Innovator

There actually is a workaround to this.
It’s a hack, but will probably work in most situations.

  1. Create a Text Field and write your values to it separated by comma.
  2. Create an automation that fires when changes are being made to that Text Field. This automation can then update your Multiple Select Field with the values from the Text Field and magically knows how to handle them.

Screen Shot 2021-09-24 at 11.49.41

Scripting was recently updated to include the ability to add options. The documentation has more details.

Overengineering award of the day goes to you for sure, :slightly_smiling_face: but like kuovonne pointed out already, this has become trivial do accomplish directly as of recently:

const 
    targetField = base.getTable('code')
        .getField('yourSingleSelFieldName'),
    tfChoices = targetField.options.choices,
    name = 'Option Potato';

if(!tfChoices.map(choice => choice.name).includes(name))
    await targetField.updateOptionsAsync(
        {choices:[...tfChoices,{name}]}
        );

You can even specify the coloring like so:

{name,color:'cyanLight2'}

Just remember to use the spread operator so as to not overwrite the values. Or maybe there’s a built-in failsafe for that here. Forgot the details for this specific use case but some mutation methods do have a degree of protections that throw early based on ‘lol, no, trust me, you didn’t want that’ reasoning. The implementation isn’t perfectly consistent just yet, but I think we can hardly have it any other way when it comes to fresh-from-the-dev-channel features.

ROARK
6 - Interface Innovator
6 - Interface Innovator

Thanks for the award. :grinning:

I saw it in the documentation already, however I cannot get it to work.
All I get is this error message: “Error: Cannot update field config from scripting automation”

@kuovonne Am I doing something terribly wrong or is this really not available in a scripting automation?

Screen Shot 2021-09-26 at 15.07.53

Updating field options might not be available in automations, although the documentation does not seem to address this issue. It may be that the documentation is missing this information.

For comparison, creating new fields is only available in Scripting app (not automation scripts).

If the code works in Scripting app, but not an automation script, contact support and explain the situation. (I cannot test this myself now as I am away from a computer.)

I am also trying to do the same as @ROARK and get the same error - it would be nice to have the ability to add choices to a multiselect from an automation script without having to jump through hoops.