Skip to main content
Solved

Using javascript to insert multiple select values into a new record

  • January 17, 2023
  • 11 replies
  • 181 views

Forum|alt.badge.img+3

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.

 

 

Best answer by TheTimeSavingCo

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



11 replies

TheTimeSavingCo
Forum|alt.badge.img+31
  • Brainy
  • 6459 replies
  • Answer
  • January 17, 2023

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




Karlstens
Forum|alt.badge.img+24
  • Brainy
  • 602 replies
  • January 17, 2023

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.


Alexey_Gusev
Forum|alt.badge.img+25
  • Brainy
  • 1261 replies
  • January 17, 2023

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

 


Karlstens
Forum|alt.badge.img+24
  • Brainy
  • 602 replies
  • January 18, 2023

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.


Forum|alt.badge.img+3
  • Author
  • New Participant
  • 4 replies
  • January 18, 2023

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




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


Forum|alt.badge.img+3
  • Author
  • New Participant
  • 4 replies
  • January 18, 2023

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


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


Forum|alt.badge.img+3
  • Author
  • New Participant
  • 4 replies
  • January 18, 2023

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.


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


Karlstens
Forum|alt.badge.img+24
  • Brainy
  • 602 replies
  • January 18, 2023

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


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


TheTimeSavingCo
Forum|alt.badge.img+31

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
Forum|alt.badge.img+25
  • Brainy
  • 1261 replies
  • January 24, 2023

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. 

 

 


Karlstens
Forum|alt.badge.img+24
  • Brainy
  • 602 replies
  • January 24, 2023

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

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