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 to show or hide based on multiselect option set values

(1) ShareShare
ReportReport
Posted on by 25

How to show or hide based on multiselect option set values

I have the same question (0)
  • Verified answer
    Wahaj Rashid Profile Picture
    11,321 on at

    Hi,

    Thank you for your query.

    To show/hide fields based on Multi-select optionset, you need to register a JavaScript function.

    For example, you can register a JS function on change of Multi-select optionset field.

    Here is a sample function to show/hide telephone1 field based on selected value:

    function onMultiSelectChange(executionContext) {
    
        const formContext = executionContext.getFormContext();
        
        const moField = formContext.getAttribute("new_multioptionsetfieldname");
        
        //Returns Array of Selected OptionSet Values, example [1001,1002,1005 ]
        const selectedOptions = moField.getSelectedOption();
    
        // Now you can implement your logic here, for example:
        
        if(selectedOptions.filter(i => i.value === 1001).length > 0) {
        
            // Hide mobile phone field
            formContext.getControl("telephone1").setVisible(false);
        
        } else {
            // Show mobile phone field
            formContext.getControl("telephone1").setVisible(true);
        }
    }

    Please note, when you register this function, you need to pass Execution Context as first parameter.

    Here are few links for your understanding:

    Get and Set Multiselect Option set field values through Javascript | Microsoft Dynamics CRM / 365 (wordpress.com)

    Hiding and Showing a Field in Dynamics 365 using JavaScript - Carl de Souza

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Kruthi2704,

    You can use businee rule to show or hide fields based on another multiselect optionset fields value, which is a no-code way.

    Go Settings > Customizations > Customize the system> Entities >Expand one entity you need > Business Rules.

     1738.pastedimage1618299899991v1.png

    8535.pastedimage1618299910863v2.png

    Save and Activite it, then publish all customizations.

    2.Test:

    When 'Relation type' field value is 'competitor', the parent account field will be hidden, otherwise, it will be shown in the form.

    2350.pastedimage1618300085697v3.png

    You can refer following link to know more about business rules:

    Create business rules and recommendations | Microsoft Docs

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hello Leah Ju, 

    I don't think a Business Rule can implement this scenario. Since Business Rules does not support multi-select option sets. Please refer to the documentation: 

    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/create-business-rules-recommendations-apply-logic-form

    0624.pastedimage1618305424209v1.png

    this need can only be implemented with JavaScript code as suggested by Wahaj Rashid. 

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Kruthi2704,

    As Mehdi said, Business Rules does not support multi-select option sets.

    I'm so sorry that I didn't notice this, you can only use js code to achieve it in the scenario.

    But if you want to show/hide fields in other scenario that fields is not multiselect option set values, Business Rules is a good way to achieve it.

    Not just show/hide fields, it has following other actions:

     7183.pastedimage1618364451280v1.png

    Consider using it the next time if you have one of these needs.

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Suggested answer
    Kruthi2704 Profile Picture
    25 on at

    Thanks wahaj

  • MattBarker Profile Picture
    25 on at

    Wahaj Rashid, This is brilliant thank you. Would you please be able to provide an example where you make multiple fields visible based on a multi-select optionset? Is it simply a case of listing multiple if statements? A revised example would be amazing.

  • MattBarker Profile Picture
    25 on at

    Sorry. i worked it out. i also added a handler for NULL.

    function onMultiSelectChange(executionContext) {

       const formContext = executionContext.getFormContext();

       const moField = formContext.getAttribute("mso_field");

       //Returns Array of Selected OptionSet Values, example [1001,1002,1005 ]

       const selectedOptions = moField.getSelectedOption();

       // Now you can implement your logic here, for example:

    if(selectedOptions != null){  

       if(selectedOptions.filter(i => i.value === 100000000).length > 0) {

           // Hide mobile phone field

           formContext.getControl("field1").setVisible(true);

       } else {

           // Show mobile phone field

           formContext.getControl("field1").setVisible(false);

       }

    if(selectedOptions.filter(i => i.value === 100000001).length > 0) {

           // Hide mobile phone field

           formContext.getControl("field2").setVisible(true);

       } else {

           // Show mobile phone field

           formContext.getControl("field2").setVisible(false);

       }

    }

    if(selectedOptions == null){

    formContext.getControl("field1").setVisible(false);

    formContext.getControl("field2").setVisible(false);

    }

    }

  • Community Member Profile Picture
    on at

    Hi I have four multi select option ["favorite player", "favorite food", "First School", "Middle Name"] .

    I want to have equal number of text boxes (Answer 1 for first option selected ,Answer 2 for second option selected, Answer 3 for third answer and so on) 

    If user selects only two option then two respective answer text boxes will show. How to achieve this?

  • RugerSR762 Profile Picture
    375 on at

    Thank you for posting this.  It is a good starting point.  However, I have encountered two issues with my use case:

    1.) When there is no value in the multiselect option set (new_type), the script will throw an error

    2.) I do not think my array is properly configured as the requirement is when a certain type (i.e. "residential" is the ONLY value selected, show/req contact only while hiding customer... meanwhile if anything except residential is selected show both customer and contact only requiring customer:

    type.png

    function displayContact(executionContext) {
    
        const formContext = executionContext.getFormContext();
    
        const oppType = formContext.getAttribute("new_type");
    
        //Returns Array of Selected OptionSet Values
        const selectedOptions = oppType.getSelectedOption();
            
        if (selectedOptions.filter(i => i.value === 925170001).length > 0) {
    
            // Display both Customer and Contact lookup fields and set required
    
            formContext.getControl("new_customerid").setVisible(true);
            formContext.getAttribute("new_customerid").setRequiredLevel("required");
            formContext.getControl("parentcontactid").setVisible(true);
            formContext.getAttribute("parentcontactid").setRequiredLevel("required");
    
        } else {
    
            // Hide Customer field and remove requirement level
    
            formContext.getControl("new_customerid").setVisible(false);
            formContext.getAttribute("new_customerid").setRequiredLevel("none");
            formContext.getControl("parentcontactid").setVisible(true);
            formContext.getAttribute("parentcontactid").setRequiredLevel("required");
        }
    }

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 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans