Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id : htIXcfjYqSmi0oq38v7+h8
Customer experience | Sales, Customer Insights,...
Suggested answer

How do I deselect selected values ​​in a multi-select field?

Like (2) ShareShare
ReportReport
Posted on 21 Mar 2025 15:42:24 by 17
Hi experts!
 
I have a multi-select field (X) that I wan't to hide / show, require / not require based on an other fields (Y) value. 
 
If field Y = 1 then show and require field X
If field Y = 0 then hide field X and set required to none
 
This is what I have achieved so far (and made sure works):
 
 
function setFieldrequirementasrequired(executionContext)
{
    var formContext = executionContext.getFormContext();
    var Y = formContext.getAttribute("column_Y").getValue();
    if (Y == 1)
    {
        formContext.getAttribute("column_X").setRequiredLevel("required");
    }
    else
    {
        formContext.getAttribute("column_X").setRequiredLevel("none");
    }
 }
 
 
function showFieldOnChange(executionContext)
{
    formContext = executionContext.getFormContext();
 
    if (formContext.getAttribute("column_Y").getValue() == 0)
    {
        formContext.getControl("column_X").setVisible(false);
 
    }
    else
    {
        formContext.getControl("column_X").setVisible(true);
    }
}
 
But, I'm struggeling with my last requirement: when field Y changes from 1 to 0 I wan't to deselect selected values in field X.
Any suggestions?
  • Suggested answer
    Daivat Vartak (v-9davar) Profile Picture
    3,128 Super User 2025 Season 1 on 22 Mar 2025 at 11:14:29
    How do I deselect selected values ​​in a multi-select field?
    Hello SA-18061446-0,
     

    You're very close! The challenge you're facing is that the getAttribute("column_X").setValue(null) approach doesn't work as expected with multi-select option sets in Dynamics 365. This is because multi-select option sets store their values as an array of option values, not as a single value.

    Here's how to correctly deselect all values in your multi-select option set (field X) when field Y changes from 1 to 0:

    Corrected Code:

    function setFieldRequirementAsRequired(executionContext) {
        var formContext = executionContext.getFormContext();
        var Y = formContext.getAttribute("column_Y").getValue();
        if (Y == 1) {
            formContext.getAttribute("column_X").setRequiredLevel("required");
        } else {
            formContext.getAttribute("column_X").setRequiredLevel("none");
        }
    }
    function showFieldOnChange(executionContext) {
        var formContext = executionContext.getFormContext();
        var Y = formContext.getAttribute("column_Y").getValue();
        if (Y == 0) {
            formContext.getControl("column_X").setVisible(false);
            // Correct way to deselect multi-select options:
            formContext.getAttribute("column_X").setValue([]); 
        } else {
            formContext.getControl("column_X").setVisible(true);
        }
    }

    Explanation of the Change:

    • formContext.getAttribute("column_X").setValue([]);

      • This line is the key. By setting the value of the multi-select attribute to an empty array ([]), you effectively clear all selected options. 

    •  

    How to Use:

    1. Add the Code as Web Resources:

      • Create two JavaScript web resources in Dynamics 365: one for setFieldRequirementAsRequired and one for showFieldOnChange. 

    2. Add the Functions to Form Events:

      • Open your form in the form editor.
      • Add the setFieldRequirementAsRequired function to the form's OnLoad event.
      • Add the showFieldOnChange function to the OnChange event of the column_Y field.
      • Add the setFieldRequirementAsRequired function to the OnChange event of the column_Y field. 

    3. Publish Customizations:

      • Publish your form customizations. 

    4.  

    Important Notes:

    • Logical Names: Ensure that column_Y and column_X are replaced with the correct logical names of your fields.
    • Testing: Thoroughly test the form to ensure that the multi-select options are cleared correctly when column_Y changes from 1 to 0.

    With this correction, your multi-select field (X) should behave as you intend:

    • When field Y is 1, field X will be visible and required.
    • When field Y is 0, field X will be hidden, not required, and any selected options will be cleared.
     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,245 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,925 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading started