I'm trying to create an automation to deselect the option 'language' from the multi-select field 'todos' in the table 'Post'
I'm doing this through a combination of automation and scripts.
I'm new to scripting and put together this by looking at some examples from the community and asking ChatGPT, but it's still not working.
Anyone so kind to give it a pass and point me in the right direction?
// Prompt the user to select a table and field
const table = await input.tableAsync('Select the table (e.g., Post)');
const singleSelectField = await input.fieldAsync('Select the single-select field (e.g., todos)', table);
const singleSelectValue = await input.textAsync('Enter the single-select option to deselect (e.g., language)');
await main();
output.markdown("# Done");
async function main() {
// get the record
let record;
while (!record) {
record = await input.recordAsync(`Select a record in the ${table.name} table`, table);
if (!record) {
output.markdown('You must select a record to continue.');
}
}
// Guard clause: Check if the field is a single-select
if (singleSelectField.type !== "singleSelect") {
output.markdown(`The field '${singleSelectField.name}' is not a single-select field.`);
output.markdown(`# Record NOT updated.`);
return;
}
// Guard clause: Check if the current value matches the value to be cleared
const currentValue = record.getCellValue(singleSelectField.id)?.name;
if (currentValue !== singleSelectValue) {
output.markdown(`The selected record does not have '${singleSelectValue}' set in the field '${singleSelectField.name}'.`);
output.markdown(`# No changes were made.`);
return;
}
// Update the record to clear the field value
await table.updateRecordAsync(record.id, {
[singleSelectField.id]: null // Clear the value
});
output.markdown(`# Cleared '${singleSelectValue}' from '${singleSelectField.name}' of record '${record.name}'.`);
}