Help

Write in new value for single select with createRecordsAsync

Topic Labels: Scripting extentions
3762 5
cancel
Showing results for 
Search instead for 
Did you mean: 
Noah_Coleman
6 - Interface Innovator
6 - Interface Innovator

Is there a way to make createRecordsAsync create a new value when it is not present in a single select field?

await table.createRecordsAsync([
        {
            fields: {
                'Print Name': prnt.name,
                'Printer': { name: prnt.printer },
            },
         }

In this example, I have two fields: ‘Print Name’ is a text field and ‘Printer’ is a single select. Is there a way to pass a value to Printer that isn’t already in the base?
This happens on the GUI side if you’re copying and pasting values. I’m wondering if you can do it inside a script.

5 Replies 5

You can do it from within the script, but I believe you would need to edit the field options before you call .createRecords:

let table = base.getTable("")
let field = table.getField("Printer")

let existingChoices = field.options.choices
let existingChoiceNames = existingChoices.map(choice => choice.name)

let choiceName = prnt.printer

if (!existingChoiceNames.includes(choiceName)) {
    await field.updateOptionsAsync({
        choices: [...existingChoices, {name: choiceName}]
    })
}

await table.createRecordsAsync([
    {fields: {
        "Print Name": prnt.name,
        [field.name]: {name: choiceName}
    }
])
Noah_Coleman
6 - Interface Innovator
6 - Interface Innovator

Thanks, @Kamille_Parks. This seems to work in the Scripting app, but if I try to run this code in an automation script, I get: Error: Cannot update field config from scripting automation. Is there another way to do this within an automation?

I just saw @Kamille_Parks answer on this thread. I don’t know that I can use the “update record” step because it can only update one record per automation run, correct? This script may be adding multiple records at a time.

If this thread is about Automations it should be categorized as such. Right now its under “Scripting App”.

Automation “run a script” actions can update multiple records at a time, but the “update record” step cannot.

Try reconsidering whether its possible to edit the single select’s options to match possible values. Why don’t the single select options already exist? Does this field have to be a single select?

Noah_Coleman
6 - Interface Innovator
6 - Interface Innovator

Apologies for the mislabeling. I’m pulling data from an API, and they (infrequently) change how their data is formatted. For example, about a year ago they changed the names of one of their materials from “Grey” to “Grey V4.” Same material, but different name and so the script threw the error.
I’ve changed some of the single select options to be text fields and added a console.log so of the existing fields so I can easily see what the options are if there is an error in the future. Thanks for explaining, Kamille!