Form.Field.MultipleOptions

On occasion a script will require the user to select one or more items from a group of items. The Form.Field.MultipleOptions class provides a mechanism for allowing the user to select specific items from a group of items displayed in the form interface.

var menuLabel = "Description of Items" var multiOptionMenu = new Form.Field.MultipleOptions( "menuKey", menuLabel, menuIndexesOrObjects, menuItemStringsOrNull, [indexesOrObjectsToShowSelected] )

In the form example shown below, the days of the week are displayed as series of labeled checkboxes. The form validation function ensures that at least one item is selected (checked) before the acceptance button (“Continue”) is enabled.

The result of the form will be an array of indexes of the selected items. Those indexes can be used to retrieve the corresponding checked-item titles or objects from a related list.

multiple-options-form
var menuItems = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"] var menuIndexes = new Array() menuItems.forEach((item,index) => {menuIndexes.push(index)}) var menuLabel = "Days of the week" var multiOptionMenu = new Form.Field.MultipleOptions( "menuKey", menuLabel, menuIndexes, menuItems, [] ) var inputForm = new Form() inputForm.addField(multiOptionMenu) var formPrompt = "Select one or more days:" var buttonTitle = "Continue" var formPromise = inputForm.show(formPrompt,buttonTitle) inputForm.validate = function(formObject){ var indexes = formObject.values["menuKey"] return (indexes.length > 0)?true:false } formPromise.then(function(formObject){ var indexes = formObject.values["menuKey"] indexes.forEach(index => { console.log(menuItems[index]) }) }) formPromise.catch(function(err){ console.error("form cancelled", err.message) })

Here's an example form using an array of objects instead of an array of strings as source for the form:

multiple-options-objects
var tasks = document.windows[0].selection.tasks if (tasks.length > 1){ // sort the task objects by name tasks.sort((a, b) => { var x = a.name.toLowerCase(); var y = b.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }) } else { throw new Error("Select two or more tasks.") } // generate a list of task names var menuItems = tasks.map(task => {return task.name}) // generate a list of matching indexes var menuIndexes = new Array() taskNames.forEach((item,index) => {menuIndexes.push(index)}) var menuLabel = "Selected Tasks" var multiOptionMenu = new Form.Field.MultipleOptions( "menuKey", menuLabel, menuIndexes, menuItems, [] ) var inputForm = new Form() inputForm.addField(multiOptionMenu) var formPrompt = "Select one or more tasks:" var buttonTitle = "Continue" var formPromise = inputForm.show(formPrompt,buttonTitle) inputForm.validate = function(formObject){ var indexes = formObject.values["menuKey"] // ensure at least one item is selected return (indexes.length > 0)?true:false } formPromise.then(function(formObject){ var indexes = formObject.values["menuKey"] indexes.forEach(index => { // process the chosen task objects console.log(tasks[index]) }) }) formPromise.catch(function(err){ console.error("form cancelled", err.message) })
var objs = document.windows[0].selection.tasks if (objs.length > 1){ // sort the objects by name objs.sort((a, b) => { var x = a.name.toLowerCase(); var y = b.name.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; }) } else { throw new Error("Select two or more objects.") } var menuLabel = "Selected Objects" var multiOptionMenu = new Form.Field.MultipleOptions( "menuKey", menuLabel, objs, null, [] ) var inputForm = new Form() inputForm.addField(multiOptionMenu) var formPrompt = "Select one or more objects:" var buttonTitle = "Continue" var formPromise = inputForm.show(formPrompt,buttonTitle)

DISCLAIMER