How to select all the option in the multipledropdown field by default and also provide "select all" option in dropdown

Certified Associate Developer

Hi All,

i want to know , how to configure the below logic

BY default all the option should be selected. and also in dropdown i can have the option to deselect all /select all along with all the options.

Please help me how i can go about this

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    You can pre-populate a local variable with the the choice value array data and map this variable in value and saveinto of the dropdown. When the form loads your local will have all the dropdown values in it. And the dropdown will also show all the values pre-selected. Users can then deselct/select the values as needed. 

    a!localVariables(
      local!language: {
        "en_US",
        "es_ES",
        "fr_FR",
        "de_DE"
      },
      a!multipleDropdownField(
        label: "Language",
        instructions: "Which language(s) are you proficient in?",
        choiceLabels: {
          "English",
          "Spanish",
          "French",
          "German"
        },
        choiceValues: {
          "en_US",
          "es_ES",
          "fr_FR",
          "de_DE"
        },
        value: local!language,
        saveInto: local!language,
        placeholder: "Select a language",
        searchDisplay: "ON"
      )
    )

  • 0
    Certified Associate Developer

    Hey

    This code helps achieve the functionality you're looking for using a multiple dropdown. All the options are stored in a local variable and preselected by default. To deselect, you can simply click the “×” icon on the multiple dropdown field, which clears the selection. This gives a clean and user-friendly way to toggle selections without extra UI controls.

  • 0
    Certified Associate Developer
    in reply to Megha Ramnani

    yeah thanks for the code. but what if user want to select all the options again. customer want option as select all and deselect all along with the options

  • 0
    Certified Associate Developer
    in reply to Anitha Dharmalingam

    Well in that case try this code ,this can be a workaround the intended functionality

    a!localVariables(
      local!allOptions: {
        1, 2, 3, 4
      },
    
      local!selectedOptions: local!allOptions, /* Default: all selected */
    
      local!selectAll: true, /* Controls checkbox */
    
      {
        a!checkboxField(
          label: "Select All",
          choiceLabels: {"All"},
          choiceValues: {true},
          value: if(count(local!selectedOptions) = count(local!allOptions), {true}, {}),
          saveInto: {
            a!save(
              local!selectedOptions,
              if(
                or(a!isNullOrEmpty(save!value) , length(save!value) = 0),
                {}, /* Deselect all */
                local!allOptions /* Select all */
              )
            )
          }
        ),
    
        a!multipleDropdownField(
          label: "Options",
          choiceLabels: {"Option 1", "Option 2", "Option 3", "Option 4"},
          choiceValues: local!allOptions,
          value: local!selectedOptions,
          saveInto: {
            a!save(local!selectedOptions, save!value)
          }
        )
      }
    )

  • 0
    Certified Associate Developer
    in reply to Megha Ramnani

    Hi Meha... it works 70% of the expected outcome. am facing one trouble here. for example in multiple dropdown i have options like A B C D. as per above code by default all four options got selected and select all check box also got selected.

    1. user deselected some options like A C and check box got deselected. and suddenly if user want to select all the option when i click on all check box am getting error. because checkbox has one value only. can you please help me out here. i tried many things but i couldnt

  • 0
    Certified Lead Developer
    in reply to Anitha Dharmalingam

       I tried Megha's code, and it looks good to me based on your needs. Could you share the code you're using that's causing the error?

  • +1
    Certified Associate Developer
    in reply to Anitha Dharmalingam

    Hi Anitha,

    The overall approach in above code works as expected just as you explained not sure what is the issue you are facing! I just made a few small tweaks to improve stability.

    Here’s the updated version:

    a!localVariables(
      local!allOptions: {1, 2, 3, 4},
      local!selectedOptions: {1, 2, 3, 4},
      local!selectAll: true,
    
      {
        /* Checkbox Field for Select All / Deselect All */
        a!checkboxField(
          label: if(local!selectAll, "Deselect All", "Select All"),
          choiceLabels: {""},
          choiceValues: {true},
          value: if(local!selectAll, {true}, {}),
          saveInto: {
            a!save(
              local!selectedOptions,
              if(
                a!isNullOrEmpty(save!value),
                {}, /* Deselect all */
                local!allOptions /* Select all */
              )
            ),
            a!save(local!selectAll, not(a!isNullOrEmpty(save!value)))
          }
        ),
    
        /* Multiple Dropdown Field */
        a!multipleDropdownField(
          label: "Options",
          choiceLabels: {"A", "B", "C", "D"},
          choiceValues: local!allOptions,
          value: local!selectedOptions,
          saveInto: {
            a!save(local!selectedOptions, save!value),
            a!save(
              local!selectAll,
              count(save!value) = count(local!allOptions)
            )
          }
        )
      }
    )

  • 0
    Certified Lead Developer

    With some saveInto manipulation, we can accomplish the original use case request of having an item in the dropdown that overrides the user's existing choices in a context-sensitive manner (here, it selects all entries EXCEPT when all entries are all selected, in which case the behavior changes to deselecting all).

    With this, I'd caveat that a better user experience would be to use either a checkbox or a dynamic, rich-text-field-based link to add this behavior near the dropdown, because even when the dropdown works fine it's a little unintuitive in terms of User Experience.  But it's an interesting exercise so I'm posting my results here for everyone's review.

    Some additional notes: instead of hardcoding the language names and codes over and over gain, I start out by creating a map array of each language and its corresponding code/label.  This allows us to quickly set the default value to all extant options, plus to set another variable that stores what the "all selected" value looks like.




    a!localVariables(
      local!languageChoices: {
        a!map(code: "en_US", name: "English"),
        a!map(code: "es_ES", name: "Spanish"),
        a!map(code: "fr_FR", name: "French"),
        a!map(code: "de_DE", name: "German")
      },
      local!allChoiceCodes: local!languageChoices.code,
      
      local!selectedLanguages: local!allChoiceCodes,
      
      local!areAllCurrentlySelected: a!isNullOrEmpty(symmetricdifference(local!allChoiceCodes, local!selectedLanguages)),
      
      
      a!multipleDropdownField(
        label: "Language",
        instructions: "Which language(s) are you proficient in?",
        choiceLabels: {
          local!languageChoices.name,
          if(
            local!areAllCurrentlySelected,
            "[Click to Deselect All]",
            "[Click to Select All]"
          )
        },
        choiceValues: {
          local!languageChoices.code,
          "all"
        },
        value: local!selectedLanguages,
        saveInto: {
          a!save(
            local!selectedLanguages,
            if(
              contains(save!value, "all"),
              if(
                local!areAllCurrentlySelected,
                tostring({}),
                local!allChoiceCodes
              ),
              save!value
            )
          )
        },
        placeholder: "Select a language",
        searchDisplay: "ON"
      )
    )