Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Visibility based on value selected in multi-select field not working

(0) ShareShare
ReportReport
Posted on by 70

Hi,

I have a multi-select choice field and based on the value selected from that field, I want to show/hide another field. So I want to show another field only if a particular value is selected from the dropdown. Here is the code I wrote, but its not working. So can you please tell me what is wrong with my code...

function hideFieldsReviewer(executionContext) {
var formContext = executionContext.getFormContext();
var agencytypecodevalue = formContext.getAttribute(“new_agencypersontypecode”).getValue();

if (agencytypecodevalue != null && agencytypecodevalue != 248870016 ) {
formContext.getControl(“new_reviewertype”).setVisible(false);

}

else {
formContext.getControl("new_reviewertype”).setVisible(true);
}

}

Thanks!

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Visibility based on value selected in multi-select field not working

    Business Rules doesnt work for Multi-Select Choice fields.

  • Suggested answer
    D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Visibility based on value selected in multi-select field not working

    Yuri ji

    Thank you for your response. Yes you are right, there is something off about those quotes which seems to have caused an error. I have rectified it, and have also realized that the main problem in my code existed in this line, which I have updated and now my code seems to be working fine…

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

    Thanks for your help! I'm marking your answer too as Suggested Answer as you pointed out the error in those quotes.

  • Suggested answer
    D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Visibility based on value selected in multi-select field not working

    Bipin Kumar 

    Thank you for your response. I tried replacing != with === which partly rectified my code, but still it wasnt working so I tried putting the value portion in brackets and wierdly it seemed to work!

    This was my updated version :-

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

    Thanks for your help! I'm marking your answer as Suggested Answer as it partly solved my problem.

  • MuhammadAnas Profile Picture
    MuhammadAnas 15 on at
    RE: Visibility based on value selected in multi-select field not working

    Why don't use Business? I think it is a good approach.

  • Suggested answer
    Yuri ji Profile Picture
    Yuri ji on at
    RE: Visibility based on value selected in multi-select field not working

    Hi,

    When I copied your code into “VS Code”, your quotation marks were not English characters.

    5140.png

    After I corrected and tested the code, I found that it works.

    Match “else” condition.

    pastedimage1663220502932v1.png

    Match “if” condition.

    pastedimage1663220532604v2.png

    Match “if(null)” condition.

    pastedimage1663220556312v3.png

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,959 Super User 2024 Season 1 on at
    RE: Visibility based on value selected in multi-select field not working

    Hi,

    It should work. Can you please share screenshot of your optionset field values to confirm if value mentioned in your js code is correct.

    Can you also check as below

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

    {

    Show field

    }

    else

    {

    Hide field

    }

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Visibility based on value selected in multi-select field not working

    I have revised the above code and updated it, but the updated code is also not working. Please find below the updated code...

    function onMultiSelectChange(executionContext) {

       var formContext = executionContext.getFormContext();

       var moField = formContext.getAttribute(“new_agencypersontypecode”);

       var selectedOptions = moField.getSelectedOption();

       if(selectedOptions != null){  

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

           formContext.getControl(“new_reviewertype”).setVisible(false);

       } else {

           formContext.getControl(“new_reviewertype”).setVisible(true);

       }

    }

    if(selectedOptions == null){

           formContext.getControl(“new_reviewertype”).setVisible(false);

    }

    }

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans