Help

Re: Using javascript to insert multiple select values into a new record

Solved
Jump to Solution
721 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Conor_McKeever
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello,

I am setting up a record to duplicate a record (but only certain fields) in a base by taking input variables and then creating a new record and inputting these into the new record as such

let recordIdNew = await table.createRecordAsync({

    "DateTest": StartDate,  

    "SingleLineText": SingleLineText,

    "PhoneNumberTest": PhoneNumberTest,

    "CheckBoxTest": CheckBoxTest,

    "LongTextTest": LongTextTest,

    "EmailTest": EmailTest,

    "SingleSelectTest": {name: SingleSelectTest},

});

 

I have managed to achieve this with all the required data formats but I’m really struggling with Multiple Select.  I am able to pull the data from the original records field and it shows in the console as follows

CONSOLE.LOG

  1. (2) ["AA""BB"]

 

The issue is, I don’t know how to add it to the new record (like I am able to do with all of the other data types)

 

I have had a look at the write data format but it doesn’t really make any sense to me at this early stage of my AirTable career.

TYPEDEF

Array<{id: string} | {name: string}>

 

I think I need to map this to a new object in the following format which I have done

CONSOLE.LOG

  1. (2) [Object, Object]
    1. ▶0: Object
      1.  name: "AAA"
    2. ▶1: Object
      1.  name: "BBB"

 

Any advice would be greatly appreciated.

 

 

1 Solution

Accepted Solutions

If you're doing a `getCellValue` to grab the options in the multiple select field, you should end up with an array of objects, each one representing a multiple select option, and it should have a "name" value

When inserting values into a multiple select field, the format's `[{name: **NAME OF OPTION**}]`, and if there's multiples it'll be `[{name: **NAME OF OPTION**}, {name: **NAME OF OPTION**}]`

And so you'll just need to loop through that array of multiple select options, and do an `array.push({name: selectOption.name})` or some such

Here's some code to get you going, and a link to a base where it's set up as an extension

let table = base.getTable('Table 1')
let record = await table.selectRecordAsync('recTF42yVfDDwGlWl')
let values = record.getCellValue('Multiple Select Field')

let options = new Array

for (let value of values){
    options.push({name: value.name})
}

await table.createRecordAsync({
    "Multiple Select Field": options
})



See Solution in Thread

11 Replies 11

If you're doing a `getCellValue` to grab the options in the multiple select field, you should end up with an array of objects, each one representing a multiple select option, and it should have a "name" value

When inserting values into a multiple select field, the format's `[{name: **NAME OF OPTION**}]`, and if there's multiples it'll be `[{name: **NAME OF OPTION**}, {name: **NAME OF OPTION**}]`

And so you'll just need to loop through that array of multiple select options, and do an `array.push({name: selectOption.name})` or some such

Here's some code to get you going, and a link to a base where it's set up as an extension

let table = base.getTable('Table 1')
let record = await table.selectRecordAsync('recTF42yVfDDwGlWl')
let values = record.getCellValue('Multiple Select Field')

let options = new Array

for (let value of values){
    options.push({name: value.name})
}

await table.createRecordAsync({
    "Multiple Select Field": options
})



Another thing to note with Single and Multiselect fields and scripting within either Script Extension App, or an Automation Scripting Action (both are very similar) - is there's a limitation that prevents the adding of new selection options to either Single Select or Multilple Select fields - which is super frustrating.

It means that, if you're writing scripts to interact with these fields, then the fields will have to be pre-fileld with all the expected values that will be available to records for the scripts to then use. If you try pushing a value that hasn't been prefilled, you'll get an error.

I am hoping Airtable address this limitation this year - but I haven't read into the limitation, or put too much thought to as to why it's a limitation to begin with, as perhaps there's a logical reason.

Hi,

Maybe I misunderstood something, but I've added new SingleSelect options without any problems.I did it several times and last was yesterday. You can't remove choices by script unless you pass parameter {enableSelectFieldChoiceDeletion: true} , so you have to append new value(s) to existing value(s).

existing value (i mean field options) is array of objects having three parameters:  {name: Xxxxx, color: lightGreen, id: 3u7DacNf}

as you can see, you need to pass id and/or name to identify your choice for cell, or you need to pass name and color to copy choices for field options, and their color schema.

that's how I copy names and colors of single select from one table to another (the linter is shining red with that code, but it's working):

 

 

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))]});

 

Ahh, yes, I may be wrong in that the limitation that I'm thinking of might strictly be within the Automation Scripting environment.

That worked perfectly, and instead of using the input variables for the rest i have have changed them all to getCellValue too.

Yes, i tried something like this in and it threw an error saying it wasnt available in automation mode.

As i am duplicating an existing record, i wont need to add new ones thankfully.   Thanks for your reply!

Could you do me a favour, and ask Airtable support to add this feature to Automation scripting? 😂

Heh yeah I hit this issue you're talking about in an automation script too

I think I ended up doing some crazy thing with an API call to the same base to create a record with the new select option values (which works, because it's the API I guess), and then deleting that record right after hah

Alexey_Gusev
10 - Mercury
10 - Mercury

I had such case in Automation too. Instead of creating record by script, I finished code with some 'output.set's, using these values in a next 'Create record' step. 

Alexey_Gusev_0-1674542837449.png

 

 

Kamille covers this topic (and limitation) well in the latest BuiltOnAir Podcast;

https://www.youtube.com/watch?v=81GDyLVFAy8

airtable #on2air #builtonair 1/17/2023 - BuiltOnAir Live Podcast Full Show - S13-E02 ___________________________ The BuiltOnAir podcast is a live weekly show highlighting everything happening in the Airtable universe. Check us out at BuiltOnAir.com/join. Join our community, join our Slack channel