Help

Re: Can I delete an option in a single select field after it has been chosen using a script

1044 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Nocodeman
5 - Automation Enthusiast
5 - Automation Enthusiast

Would using this documentation: 
In the updated code, we first get the select field using table.getField(timeFieldName) and then update the options using the updateOptionsAsync method. We spread the current choices using ...selectField.options.choices and then add the updated option using {...updatedOption}. We also pass an object with enableSelectFieldChoiceDeletion: true as the second argument to allow choices to be deleted.

Allow me to get what I want. My idea is to create a non conflict scheduler using a single dropdown field with all of the times and after that time has been chosen and a form submitted. The script will run and can either replace the option of the time and say "booked" or delete the option in the field totally. The script runs but does not do anything. I tried to logic around and tried to get the ID of the option and replace it but nothing has been really working. If there is another way to create a non-conflicting amenity reservation system let me know but that is my goal here. 


let config = input.config();
let tableName = config.tableName;
let viewName = config.viewName;
let timeFieldName = config.timeFieldName;
let formSubmission = config;
let timeSelected = config.timeSelected;

console.log('timeSelected:', timeSelected);
// Check if timeSelected matches the desired format
if (!timeSelected.match(/^\d{1,2}:\d{2} (AM|PM)$/)) {
console.error("Invalid timeSelected format. Please use the format 'HH:MM AM/PM'.");
return;
}

let table = base.getTable(tableName);
let view = table.getView(viewName);
let queryResult = await view.selectRecordsAsync();

let recordsToUpdate = [];
let timeSelectedId = '';

for (let record of queryResult.records) {
let options = record.getCellValue(timeFieldName);
console.log('options:', options);
if (Array.isArray(options)) {
let option = options.find(opt => opt.id === timeSelected);
if (option && option.id === timeSelected) {
timeSelectedId = option.id;
let selectField = table.getField(timeFieldName);
let updatedOption = {
id: option.id,
name: 'booked'
};
let newOptions = [
...selectField.options.choices.filter((choice) => choice.id !== updatedOption.id),
updatedOption
];
recordsToUpdate.push({
id: record.id,
fields: {
[timeFieldName]: newOptions
}
});
}
}
}

console.log(`Records to update:`, recordsToUpdate);

await table.updateRecordsAsync(recordsToUpdate);

console.log(`Option ${timeSelected} in field ${timeFieldName} has been updated to "booked" for all records in ${tableName}.`);

 

3 Replies 3

Check out @Kamille_Parks on the Airtable Universe. She has some No-Conflict Reservation bases that you might find helpful. 

https://www.airtable.com/universe/creator/usr7MfEAojKaYg0DB/kamille-parks

Nocodeman
5 - Automation Enthusiast
5 - Automation Enthusiast

I took a look at the calendar which is very nice. But doesn't have the same functionality as what I need. I would like to hold my clients hand and only give them a drop-down of what options they're. I want this to live within a form which is why I need need the option in the single select field to be changed or deleted. 

Like this?

If yes, then maybe you don't need a script - you can use a form that pulls the single select options from a view that filters for when the linked reservation field is empty.