Dec 21, 2020 07:27 AM
I am trying to develop a script with a mutli-select option. Is there a code for each of these so if I choose to change the actual name, it won’t be a problem?
Dec 21, 2020 10:16 AM
Hi @Rose_Haft1 - Single and Multi-select fields have a similar structure. A single select is an object of id, name and colour. Multi-select is an array of these objects:
You can reference the select value or values by the id rather than the name and if the name is changed the id will remain the same. When creating or updating a single select field you can write to is using the id or the name:
So, as you want to do, if you go for the id option here you will be fine.
Dec 22, 2020 04:18 AM
Thanks. That is great. Do you have a sample of how I would reference these with script? I have not been able to figure this out…
Dec 22, 2020 01:38 PM
@Rose_Haft1 - if I have two single select fields like this:
Then this script will get the values of each value in the first field, then updates the second field with its value:
let table = base.getTable('Table 1');
let query = await table.selectRecordsAsync();
// read the values
for (let record of query.records) {
let id = record.getCellValue('Single Select').id;
let name = record.getCellValue('Single Select').name;
let color = record.getCellValue('Single Select').color;
// read/log the values
console.log(id, name, color)
// write the values
table.updateRecordAsync(record, {
'Single Select 2': {id: id}
// or:
// 'Single Select 2': {name: name}
})
}
It is similar for a multi-select, but you would have to iterate over each value within the field:
record.getCellValue('Multi Select')
Jul 06, 2021 01:26 PM
Is there a way to reference the list of potential single-select values? If so, this could be used to simulate a dropdown single-select using the button input.
Jul 06, 2021 02:18 PM
You can get the values from the field options.
You don’t actually need the select ids. I usually map the single select choice names to the choices for an input button.
Jul 07, 2021 06:55 AM
Thank you for calling this out!
I usually map the single select choice names to the choices for an input button.
Do you have an example? :slightly_smiling_face:
Edit: Figured it out. Hopefully this helps someone else:
let exampleTable = base.getTable("table");
let fieldName = 'example dropdown'
let buttonVal = await input.buttonsAsync(`Example button label`, exampleTable.getField(fieldName).options['choices'].map(choice => {
return {
'label': Object.values(choice)[1]
}
}));
Oct 19, 2021 04:16 AM
Thank you. Are these all the properties that can be accessed?
Oct 19, 2021 04:53 AM
Yes. They are all that are listed in the documentation. Note that the color might not exist if the options are not in color. You can also determine the order of the options from the order of the choices in the array.
Were you hoping for additional properties? If so, which ones?