CRM 2011 Javascript for hiding a field based on another fields value

This script will hide or show a certain fields control based on the value of another field on the form. In this sample a ‘Two Option’ field is used to determine if a dropdownlist should be hidden. Modify to suit your needs.

function SetControlInvisible(field, dependantField) {
    //get control for field
    var control = Xrm.Page.ui.controls.get(field);
    if ((control != “undefined”) && (control != null))
    {
        //get control for dependant field
        var dependantControl = Xrm.Page.ui.controls.get(dependantField);
        if ((dependantControl != “undefined”) && (dependantControl != null)) {
            var attribute = dependantControl.getAttribute();
            var depandantFieldValue = attribute.getValue();
            //set visible based on depandant fields value
            if (depandantFieldValue == false) {
                control.setVisible(false);
            }
            else {
                control.setVisible(true);
            }
        }
        else {
            alert(‘control not found ‘ + dependantField);
        }
    }
    else {
        alert(‘control not found ‘ + field);
    }
}

Register the script with the form onLoad and field onChange events. Passing the names of the fields as parameters.

image

About these ads

11 thoughts on “CRM 2011 Javascript for hiding a field based on another fields value

  1. finisterre

    Can the newly shown fields be ‘Business Required’ only when displayed? Or will I be unable to save the form if they are ‘Business Required’ but hidden?

    Reply
  2. finisterre

    I will if I ever figure out how to add a script to a form in hosted CRM 2011. I’ve yet to use the SDK!

    Thanks.

    Reply
  3. Joe

    I am new to Dynamics CRM but used it successfully, Thanks!
    although the single quotes on this page needs to be changed to the regular quote character…

    Reply
  4. Bob Hatcher

    Thanks for the post. Just a few points for future readers- the previous point about replacing the slanted quotes is quite true. MS can be squirrelly about the character set in JS, too, so be sure you are editing directly in their editor.

    Most importantly, I got hung up because I was sending the field names in the wrong order. In the final screenshot, the field you WANT TO HIDE goes first and is “field” in the code; the field you want to CONTROL the hiding is called “dependantControl”.

    Reply
  5. Pingback: Hey girl. Sometimes you have to toggle a field's visibility to get your sales buddies to enter data - Adopt2Win by Zero2Ten - CRM Technical Blogs - Microsoft Dynamics Community

  6. kawirider88

    This is a great post with functionality I would love to use. Unfortunately I cannot get this to work for the life of me. I always get an error that the function is undefined. I thought the issue at first was I was trying to use a drop down list and based on the selection would hide another field. So I tried using the two option set to hide another field and got the same results. I went though my code and the example code word for word and there are no differences. (other than the names of my fields)

    Reply
  7. Simeon Olusola Gbeleyi

    Good day, I need an urgent response on this function code please. I intended to unhide some particular field whenever an option is been select in an option set field in Dynamics Crm 2011 form. Using the code below, I it do pop up an error message saying “object expected” if the source that is the option set unchange event is being called. Anyone should kindly help me out by looking at the place where I am actually wrong in the code bellow. I will appreciate it if you can send the solution to my email addresses: simmieglobe@gmail.com, simeon.gbeleyi@ccsnl.com

    // Hide or Show field based on selected option

    function fnc_Hide_Show_Field()
    {
    var Option = Xrm.Page.getAttribute(“devbox_source”).getSelectedOption().text;
    if (Option == “Bank”)
    //example: based on the option “Individual” show field relevant to Individual
    //or based on the option “Team” show field relevant to Team.
    {
    Xrm.Page.getControl(“devbox_bank”).setVisible(true);

    }
    break;

    if (option == “Marchant”)

    {
    xrm.page.getcontrol(“devbox_marchant”).setvisible(true);

    }
    break;

    if (option == “Others”)

    {
    xrm.page.getcontrol(“devbox_others”).setvisible(true);
    }

    }

    Reply
    1. danielbergsten Post author

      Insert alert messages after every line of code to find which object is missing. Like this:

      Alert(‘line 1′);

      Alert(‘line 2′);

      Also remove your break statements.

      Reply
  8. Garabed Yegavian

    I am able to get this to work based on if the field is null or not but how to I make it work based on a specific value in the dependent field? If the service field = temporary I want to show field, otherwise hide?

    Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s