Help

Re: Unable to add new option on single select with automation script

3478 3
cancel
Showing results for 
Search instead for 
Did you mean: 
jlabbe
6 - Interface Innovator
6 - Interface Innovator

I'm using a script in an automation to try and add a new option to a single select field. As far as I can tell, everything seems setup correctly. Permissions on the field is also fine too.
Here's the script:

 

let config = input.config();
let subtaskId = config.subtaskId;
let name = config.name[0];
let color = config.color[0];

let subtasksTable = base.getTable("Subtasks");
const singleSelect = subtasksTable.getField("mySingleSelectField");

await singleSelect.updateOptionsAsync({
    choices: [...singleSelect.options.choices, {name: name, color: color}]
});

 

When I run it, I get this error:

 

Error: Cannot update field config from scripting automation
    at main on line 10

 

`name` and `color` are input variables that are looking correct. The table and field the script is referencing do exist. From that error message it sounds like this isn't possible from automation, or what exactly am I missing here?

8 Replies 8
marks
Airtable Employee
Airtable Employee

Hey @jlabbe - this is actually expected. The Field.updateOptionsAsync method is only available for the Scripting Extension and not Automations' Run Script Actions:

Screen Shot 2023-01-23 at 11.03.57 AM.png

 

I believe you can output your value from the Run Script Action and then use it in an Update Record Action and if the choice does not yet exist, it will get created for you. 

jlabbe
6 - Interface Innovator
6 - Interface Innovator

Hey @marks, thanks for the fast response!

I wasn't aware about the Scripting Extension. That looks like it unlocks a lot of new potential, so I'll be checking that out for sure.

Correct me if I'm wrong, but I think with an Update Record action you can create a new choice but not specify the color in any way? I'm trying to re-create a single select from a lookup field so I can capture its colors, so it's pretty critical that I can also pick the color the option creates with.

The Scripting Extension is powerful! One main difference between it and Run Script Actions in Automations, though, is that it is triggered manually by you or your user clicking a button -- as opposed to Automations which have other triggers.


Correct me if I'm wrong, but I think with an Update Record action you can create a new choice but not specify the color in any way?


That is correct, you cannot select the color when creating via Update Record Action. I believe your options are Scripting Extension, Web API, or Custom Extension, according to the linked docs.

jlabbe
6 - Interface Innovator
6 - Interface Innovator

@marks 
Thanks for the context.

So since Scripting Extensions have to be triggered manually, and Run Script Actions don't have permissions to create a new single select option, it's starting to feel like this is not possible as an automated solution? I don't want to do this manually by pushing a button or having to pre-create options in the single select for the script to work. (trying to get it triggered as a specific field is updated so there's no extra overhead)

Hi,

It's possible with additional automation steps. You can add more steps and use all data from previous. In scripting steps, you can add command to generate output which you can use in further steps: 

output.set('variableName', value)

For example, to override restriction of new single select option, I used scripting to get some values and then use them in next, 'native', Create record step, which can add new select value (same as Update record).

Alexey_Gusev_0-1674566619496.png

 

Regarding color, you can use that code example in Scripting extension to APPEND all single select options of Table 1, Field 1  with their colors to the setup of Table 2, Field 2 . Just update first line. Note that there is no overwrite rule for matching names, both versions will exist. 

const [T1,F1,T2,F2]=['Table1','Field1','Table2','Field2'];
const id_out=({id,...rest})=>({...rest})
await base.getTable(T2).getField(F2).updateOptionsAsync({'choices':
[...base.getTable(T2).getField(F2).options.choices,...(base.getTable(T1).
getField(F1).options.choices.map(id_out))]});

 

 

 

@Alexey_Gusev For the last portion where you are using Scripting Extension to apply the colors, how is that triggering exactly? Is that done manually, or somehow from the automation?

Sorry for misunderstanding, previous picture with automation setup has nothing to do with last portion of code. The code launched manually, single run to copy all field values.

 

Alexey_Gusev_0-1674574851687.png

 

@Alexey_Gusev Gotcha. I'm trying to make this fully automated to over any extra overhead like that.