Aug 12, 2021 09:22 AM
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.
Aug 12, 2021 10:03 AM
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}
}
])
Aug 13, 2021 05:54 AM
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?
Aug 13, 2021 05:58 AM
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.
Aug 13, 2021 07:16 AM
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?
Aug 15, 2021 05:18 AM
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!