Jul 29, 2024 01:59 AM - edited Jul 29, 2024 03:40 AM
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.`);
}
Solved! Go to Solution.
Jul 29, 2024 05:56 AM
Try updating your 'selectedOptions' line like so:
let selectedOptions = [{name: "AI"} , {name: "Augmented Reality"}];
Jul 29, 2024 05:56 AM
Try updating your 'selectedOptions' line like so:
let selectedOptions = [{name: "AI"} , {name: "Augmented Reality"}];
Jul 29, 2024 07:52 AM
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
Jul 30, 2024 09:20 AM
Cool! Thanks to all.