Jun 18, 2020 04:51 AM
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
Jun 18, 2020 08:16 AM
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.
Jun 18, 2020 09:17 AM
bummer, it would be awesome if it could create them or at least throw an error if it doesn’t exist
Jun 18, 2020 10:14 AM
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.)
Sep 24, 2021 02:57 AM
There actually is a workaround to this.
It’s a hack, but will probably work in most situations.
Sep 24, 2021 07:50 AM
Scripting was recently updated to include the ability to add options. The documentation has more details.
Sep 24, 2021 09:11 AM
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.
Sep 26, 2021 06:09 AM
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?
Sep 26, 2021 03:09 PM
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.)
Sep 27, 2021 11:55 AM
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.