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 :
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.
I have the same question (0)
  • ALAN365 Profile Picture
    on at

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

  • Suggested answer
    Eiken Profile Picture
    on at

    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

  • Verified answer
    Eiken Profile Picture
    on at

    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.

  • Verified answer
    Eiken Profile Picture
    on at

    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);
            }
        }
    }
    

  • ALAN365 Profile Picture
    on at

    It's working! Thank you so much Eiken.

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
iampranjal Profile Picture

iampranjal 41

#2
Martin Dráb Profile Picture

Martin Dráb 36 Most Valuable Professional

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans