Skip to main content

The objective of the code is to select the options that are in the array for a multiple select field. I don't understand why it never allows adding options even if they are exactly the same as those in the table. It is an automation script. Thanks in advance for the help.

 

 

let table = base.getTable("TABLE"); // the table is correct
let fieldName = "Technology";

let recordId = "recXXXXXXXXXXXXXX"; // the record id is correct

let selectedOptions = ["AI", "Augmented Reality"];

let record = await table.selectRecordAsync(recordId);

if (record) {
let currentValues = record.getCellValue(fieldName) || [];
let newValues = [
...currentValues.filter(opcion => selectedOptions.includes(opcion)),
...selectedOptions.filter(opcion => !currentValues.includes(opcion))
];
await table.updateRecordAsync(recordId, {
[fieldName]: newValues
});
console.log(`Record ${recordId} updated with options: ${newValues.join(", ")}`);
} else {
console.log(`Record with ID: ${recordId} does not exist.`);
}

 

 

 

Try updating your 'selectedOptions' line like so:

let selectedOptions = [{name: "AI"} , {name: "Augmented Reality"}];

 


Pay attention to a difference between cell read format and cell write format. 
You receive array of objects with  id, name and color (color can be absent)
To write, you should prepare array of objects with name only  (or with id only)

let currentValues = record.getCellValue(fieldName) || [];
console.log(currentValues)
let newValues = [
...currentValues.filter(opcion => selectedOptions.includes(opcion)),
...selectedOptions.filter(opcion => !currentValues.includes(opcion))
];
console.log(newValues)
let writeValues=newValues.map(value=>( {id:value.id} ))
//also can be written as: let writeValues=newValues.map( ({name,...rest})=>({name}) )
//when you put {object} in arrow-function, it must be enclosed in round brackets
await table.updateRecordAsync(recordId, {
[fieldName]: writeValues

Cool! Thanks to all.


Reply