Help

Re: Script to Deselect a Multi-select field

145 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Paolo_Perrone
6 - Interface Innovator
6 - Interface Innovator

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}'.`);
}
4 Replies 4

If you're open to a non-scripting solution, I'd suggest creating a formula field that outputs the values of the multiple select field, except it also removes the word 'language'.  You can then get the automation to paste the value from that formula field into the multiple select field and it'll do what you want

If you want to use a script for this, I'd suggest reviewing the docs yourself so that you can ask for corrections to be made.  Also try feeding the docs to your LLM of choice to try to get something working out of it, and also break out your code requests into little snippets that you can test.  For example, first get it to update the select field with an option of your choice, then get it to remove an option, etc etc

From what I can see of your code, it doesn't come close to what you're trying to do, so I'd suggest starting a new chat entirely instead of trying to fix your current one

Hi,
This is a case where level of chatGPT over-engineering can be described as very close to "sh*t-coding" 😁 (sorry)
If you need to remove  'language' from the whole field, or from given part of records which you can filter, and you want to do that as one-time action, you can create a formula with substitute, as @TheTimeSavingCo advices, but you might have problems with comma when a 'language' is last selected option in the list.
A solution, easier for beginners, is to duplicate the field, enter duplicate field settings and remove that option. Then copy-paste whole field back from duplicate to original field. You can filter records before copy-pasting, so the operation will only affect visible records.

If you really need this to be done as part of automation, I think this example is clear. Don't forget to change word 'Scripting' to your option 'language'

Alexey_Gusev_0-1733134710470.png

Alexey_Gusev_1-1733134873900.png

 



 

thanks for all the tips!

Why do you suggest the non-scripting solution? Is not clear to me what's the advantage over the scripting one.

Is it probably easier to implement for someone with non-scripting experience like me?

Thanks for providing the code!

As I asked to @TheTimeSavingCo why do you suggest the non-scripting solution over the scripting one?