web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

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

(3) ShareShare
ReportReport
Posted on by 24
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?
I have the same question (0)
  • Verified answer
    Daivat Vartak (v-9davar) Profile Picture
    7,835 Super User 2025 Season 2 on at
    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
  • SA-18061446-0 Profile Picture
    24 on at
    Hi Daivat,
     
    Thank you so much for your time and help, it works!
  • Nitesh Raj Profile Picture
    188 on at
    Hi Friend,

    I am unable to understand why you did not use business rules (BR) for your task.

    Regards,
    Nitesh Raj

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans