Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Hide/Display multiselect column based on another multiselect column selection JavaScript

(0) ShareShare
ReportReport
Posted on by

Hi expert,

Have a requirement, that hide/display multiselect column - abc_multiselect_A based on another multiselect column - abc_multiselect_B item select, if select choice value, for example: 101010003, display abc_multiselect_A, otherwise hiden it. Script as below:

    
   
function toggleMultiSelectVisibility(executionContext) {

    debugger;

    var formContext = executionContext.getFormContext(); 

    var selectedOptions = formContext.getAttribute('abc_multiselect_B').getSelectedOption();

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

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

    } else {

        formContext.getControl("abc_multiselect_A").setVisible(true);
    }
}
The error: selectedOptions = null.
Could you please help to fix? Thanks.
  • ALAN365 Profile Picture
    on at
    RE: Hide/Display multiselect column based on another multiselect column selection JavaScript

    It's working! Thank you so much Eiken.

  • Verified answer
    Eiken Profile Picture
    on at
    RE: Hide/Display multiselect column based on another multiselect column selection JavaScript

    Hi,

    I neglected to mention that you are using multiple choice and my code above uses single choice.

    The filter function is correct.

    Now I edit the code again. Please note that some of the details in the later judgement statements have also been changed

    function toggleMultiSelectVisibility(executionContext) {
        var formContext = executionContext.getFormContext(); 
        if(formContext.getAttribute('crd9d33_testmultiplechoice').getSelectedOption()==null){
            formContext.getControl("crd9d33_testoptionb").setVisible(false);
        }else{
            var selectedOptions = formContext.getAttribute('crd9d33_testmultiplechoice').getSelectedOption();
            if (selectedOptions.filter(i => i.value == 971420000).length > 0) {
                formContext.getControl("crd9d33_testoptionb").setVisible(true);
            } else {
                formContext.getControl("crd9d33_testoptionb").setVisible(false);
            }
        }
    }
    

  • Verified answer
    Eiken Profile Picture
    on at
    RE: Hide/Display multiselect column based on another multiselect column selection JavaScript

    Hi,

    I also test to using the Javascript to hide the field.

    In your code, selectedOptions will not get value when the abc_multiselect_B is enpty.

    Now i edit your code(Add the judgment statement and replace the filter function) for reference. This code will hide the test options B when choose select2 in test options A.

    function toggleMultiSelectVisibility(executionContext) {
        var formContext = executionContext.getFormContext(); 
        if(formContext.getAttribute('crd9d33_testoptiona').getSelectedOption()==null){
            formContext.getControl("crd9d33_testoptionb").setVisible(false);
        }else{
            var selectedOptions = formContext.getAttribute('crd9d33_testoptiona').getSelectedOption();
            if (selectedOptions.value!==971420001) {
                formContext.getControl("crd9d33_testoptionb").setVisible(false);
            } else {
                formContext.getControl("crd9d33_testoptionb").setVisible(true);
            }
        }
    }

    Tips: "971420001" is the value of select 2. crd9d33_testoptiona and crd9d33_testoptionb are logical name of test options.

    By the way, you need to add this code to both "On change" and "On Load" to make sure execute the event in the initial state of form.

    Results are as follow:

    pastedimage1678342082228v1.png

    pastedimage1678342090983v2.png

    pastedimage1678342108079v3.png

    test optionB only appear when choose select2 in test optionA.

  • Suggested answer
    Eiken Profile Picture
    on at
    RE: Hide/Display multiselect column based on another multiselect column selection JavaScript

    Hi,

    You can use the business rule without code.

    pastedimage1678330633730v1.png

    Like the following example, hide the test optionB when the test optionA don't equal select1.

    pastedimage1678330655603v2.png

    pastedimage1678330664336v3.png

    Result are as follow.

    pastedimage1678330838342v4.png

    pastedimage1678330996718v5.png

  • ALAN365 Profile Picture
    on at
    RE: Hide/Display multiselect column based on another multiselect column selection JavaScript

    Could anybody help to check why the variable selectedOptions cannot be assigned value? Thank you!

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,025 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,837 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans