Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Show hide attributes

Posted on by Microsoft Employee

Hi Community,

i have one option set with 5 options,

and i have 5 fields in my form,

on load option set value is zero all the fields will be hide

on change if i select option  like a the filed a only show remaining will be hide

i write using if else condition and its working

But i want to write using Switch statement

code is

function filterfields()
{
var firstPickListValue = Xrm.Page.getAttribute("new_options").getValue();
if (firstPickListValue == null)
{
Xrm.Page.ui.controls.get("new_a").setVisible(false);
Xrm.Page.ui.controls.get("new_b").setVisible(false);
Xrm.Page.ui.controls.get("new_c").setVisible(false);
Xrm.Page.ui.controls.get("new_d").setVisible(false);
Xrm.Page.ui.controls.get("new_e").setVisible(false);
}
else if(firstPickListValue == 10,00,00,000)
{
Xrm.Page.ui.controls.get("new_a").setVisible(true);
Xrm.Page.ui.controls.get("new_b").setVisible(false);
Xrm.Page.ui.controls.get("new_c").setVisible(false);
Xrm.Page.ui.controls.get("new_d").setVisible(false);
Xrm.Page.ui.controls.get("new_e").setVisible(false);
}
else if(firstPickListValue == 10,00,00,001)
{
Xrm.Page.ui.controls.get("new_b").setVisible(true);
Xrm.Page.ui.controls.get("new_a").setVisible(false);

Xrm.Page.ui.controls.get("new_c").setVisible(false);
Xrm.Page.ui.controls.get("new_d").setVisible(false);
Xrm.Page.ui.controls.get("new_e").setVisible(false);
}
else if(firstPickListValue == 10,00,00,002)
{
Xrm.Page.ui.controls.get("new_c").setVisible(true);
Xrm.Page.ui.controls.get("new_a").setVisible(false);
Xrm.Page.ui.controls.get("new_b").setVisible(false);

Xrm.Page.ui.controls.get("new_d").setVisible(false);
Xrm.Page.ui.controls.get("new_e").setVisible(false);
}
else if(firstPickListValue == 10,00,00,003)
{
Xrm.Page.ui.controls.get("new_d").setVisible(true);
Xrm.Page.ui.controls.get("new_a").setVisible(false);
Xrm.Page.ui.controls.get("new_b").setVisible(false);
Xrm.Page.ui.controls.get("new_c").setVisible(false);

Xrm.Page.ui.controls.get("new_e").setVisible(false);
}
else if(firstPickListValue == 10,00,00,004)
{
Xrm.Page.ui.controls.get("new_e").setVisible(true);
Xrm.Page.ui.controls.get("new_a").setVisible(false);
Xrm.Page.ui.controls.get("new_b").setVisible(false);
Xrm.Page.ui.controls.get("new_c").setVisible(false);
Xrm.Page.ui.controls.get("new_d").setVisible(false);

}
}

 

 

*This post is locked for comments

  • Suggested answer
    Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: Show hide attributes

    The code would be

    // hide the controls
    var ctls=["new_a","new_b","new_c","new_d","new_e"]; for (i=0; i < ctls.length; i++){ Xrm.Page.ui.controls.get(ctls[i]).setVisible(false); }


    but I think the 5 separate queries make it clearer what is going on...

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Show hide attributes

    Hi,

    Is there any possibility to write this below code in single for loop

    function filterfields() {

       Xrm.Page.ui.controls.get("new_a").setVisible(false);

       Xrm.Page.ui.controls.get("new_b").setVisible(false);

       Xrm.Page.ui.controls.get("new_c").setVisible(false);

       Xrm.Page.ui.controls.get("new_d").setVisible(false);

       Xrm.Page.ui.controls.get("new_e").setVisible(false);

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Show hide attributes

    Thank you so much Ben, its very helpful

  • Verified answer
    Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: Show hide attributes

    Personally I would be using

    function filterfields() {
        Xrm.Page.ui.controls.get("new_a").setVisible(false);
        Xrm.Page.ui.controls.get("new_b").setVisible(false);
        Xrm.Page.ui.controls.get("new_c").setVisible(false);
        Xrm.Page.ui.controls.get("new_d").setVisible(false);
        Xrm.Page.ui.controls.get("new_e").setVisible(false);
    
        var firstPickListValue = Xrm.Page.getAttribute("new_options").getValue();
        switch (firstPickListValue) {
            
            case '100000000':
                Xrm.Page.ui.controls.get("new_a").setVisible(true);
               
                break;
            case '100000001':
                Xrm.Page.ui.controls.get("new_b").setVisible(true);
                break;
            default:
                break;
    
        }
      
    }

    I know it means you are setting the same value twice but personally it's far easier to read and follow what is happening within the code...

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Show hide attributes

    If you want to implement switch case then  here is sample code  -

    function filterfields() {
        var firstPickListValue = Xrm.Page.getAttribute("new_options").getValue();
        switch (firstPickListValue) {
            case null:
                Xrm.Page.ui.controls.get("new_a").setVisible(false);
                Xrm.Page.ui.controls.get("new_b").setVisible(false);
                Xrm.Page.ui.controls.get("new_c").setVisible(false);
                Xrm.Page.ui.controls.get("new_d").setVisible(false);
                Xrm.Page.ui.controls.get("new_e").setVisible(false);
                break;
            case '100000000':
                Xrm.Page.ui.controls.get("new_a").setVisible(false);
                Xrm.Page.ui.controls.get("new_b").setVisible(false);
                Xrm.Page.ui.controls.get("new_c").setVisible(false);
                Xrm.Page.ui.controls.get("new_d").setVisible(false);
                Xrm.Page.ui.controls.get("new_e").setVisible(false);
                break;
            case '100000001':
                Xrm.Page.ui.controls.get("new_a").setVisible(true);
                Xrm.Page.ui.controls.get("new_b").setVisible(false);
                Xrm.Page.ui.controls.get("new_c").setVisible(false);
                Xrm.Page.ui.controls.get("new_d").setVisible(false);
                Xrm.Page.ui.controls.get("new_e").setVisible(false);
                break;
            default:
                Xrm.Page.ui.controls.get("new_a").setVisible(false);
                Xrm.Page.ui.controls.get("new_b").setVisible(false);
                Xrm.Page.ui.controls.get("new_c").setVisible(false);
                Xrm.Page.ui.controls.get("new_d").setVisible(false);
                Xrm.Page.ui.controls.get("new_e").setVisible(false);
    
        }
      
    }
    


  • Suggested answer
    sandeepstw Profile Picture
    sandeepstw 4,601 on at
    RE: Show hide attributes

    HI,

    var Value = Xrm.Page.getAttribute("new_options").getValue();

    switch(Value) {

       case 10,00,00,000:

           code block

           break;

       case case 10,00,00,001:

           code block

           break;

    ------other blocks -----

       default:

           Xrm.Page.ui.controls.get("new_a").setVisible(false);

          Xrm.Page.ui.controls.get("new_b").setVisible(false);

          Xrm.Page.ui.controls.get("new_c").setVisible(false);

         Xrm.Page.ui.controls.get("new_d").setVisible(false);

         Xrm.Page.ui.controls.get("new_e").setVisible(false);

         break;

    }

    use default for null value.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Show hide attributes

    Hi Thakyou Goutam,

    Its working with if and else if condition but i want this code using Switch condition, Is it possible

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Show hide attributes

    Hi,

    Remove the comma(,) from the value - Try with this  -

    function filterfields() {
        var firstPickListValue = Xrm.Page.getAttribute("new_options").getValue();
        if (firstPickListValue == null) {
            Xrm.Page.ui.controls.get("new_a").setVisible(false);
            Xrm.Page.ui.controls.get("new_b").setVisible(false);
            Xrm.Page.ui.controls.get("new_c").setVisible(false);
            Xrm.Page.ui.controls.get("new_d").setVisible(false);
            Xrm.Page.ui.controls.get("new_e").setVisible(false);
        }
        else if (firstPickListValue == "100000000") {
            Xrm.Page.ui.controls.get("new_a").setVisible(true);
            Xrm.Page.ui.controls.get("new_b").setVisible(false);
            Xrm.Page.ui.controls.get("new_c").setVisible(false);
            Xrm.Page.ui.controls.get("new_d").setVisible(false);
            Xrm.Page.ui.controls.get("new_e").setVisible(false);
        }
        else if (firstPickListValue == "100000001") {
            Xrm.Page.ui.controls.get("new_b").setVisible(true);
            Xrm.Page.ui.controls.get("new_a").setVisible(false);
    
            Xrm.Page.ui.controls.get("new_c").setVisible(false);
            Xrm.Page.ui.controls.get("new_d").setVisible(false);
            Xrm.Page.ui.controls.get("new_e").setVisible(false);
        }
        else if (firstPickListValue == "100000002") {
            Xrm.Page.ui.controls.get("new_c").setVisible(true);
            Xrm.Page.ui.controls.get("new_a").setVisible(false);
            Xrm.Page.ui.controls.get("new_b").setVisible(false);
            Xrm.Page.ui.controls.get("new_d").setVisible(false);
            Xrm.Page.ui.controls.get("new_e").setVisible(false);
        }
        else if (firstPickListValue == "100000003") {
            Xrm.Page.ui.controls.get("new_d").setVisible(true);
            Xrm.Page.ui.controls.get("new_a").setVisible(false);
            Xrm.Page.ui.controls.get("new_b").setVisible(false);
            Xrm.Page.ui.controls.get("new_c").setVisible(false);
    
            Xrm.Page.ui.controls.get("new_e").setVisible(false);
        }
        else if (firstPickListValue == "100000004") {
            Xrm.Page.ui.controls.get("new_e").setVisible(true);
            Xrm.Page.ui.controls.get("new_a").setVisible(false);
            Xrm.Page.ui.controls.get("new_b").setVisible(false);
            Xrm.Page.ui.controls.get("new_c").setVisible(false);
            Xrm.Page.ui.controls.get("new_d").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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans