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 CRM (Archived)

hiding fields dynamically

(0) ShareShare
ReportReport
Posted on by

Hello,

I need some help with some Javascript.

What I am trying to is hide either the Company or the Partner lookup field depending on what is selected in the Contact Type drop down list.

  Figure1   

For example, as Figure 2, when I select Customer from the drop down list, I want to make the Partner field disappear as shown in Figure 3.

Figure2  Figure3 

 

Here's the problem, when I save & close and then open up the form, the Partner field comes back:

Figure4   

Here is the code on the OnLoad form event:

if(crmForm.all.new_contacttype.DataValue != null)

{

crmForm.all.new_contacttype.FireOnChange();

}

 

And here is the code on the OnChange event for the Contact Type field:

if(crmForm.all.new_contacttype.DataValue == 2)  

{

   crmForm.all.parentcustomerid.DataValue = null;

   crmForm.all.parentcustomerid.ForceSubmit = true;

  crmForm.all.parentcustomerid.style.display= "none";

crmForm.all.parentcustomerid_c.style.display= "none";

crmForm.all.parentcustomerid_d.style.display= "none";

 

  crmForm.all.new_partnerid.style.display= "";

  crmForm.all.new_partnerid_c.style.display= "";

  crmForm.all.new_partnerid_d.style.display= "";

}

 

if(crmForm.all.new_contacttype.DataValue == 1) 

{

   crmForm.all.new_partnerid.DataValue = null;

   crmForm.all.new_partnerid.ForceSubmit = true;

  crmForm.all.parentcustomerid.style.display= "";

  crmForm.all.parentcustomerid_c.style.display= "";

  crmForm.all.parentcustomerid_d.style.display= "";

  crmForm.all.new_partnerid.style.display= "none";

  crmForm.all.new_partnerid_c.style.display= "none";

  crmForm.all.new_partnerid_d.style.display= "none";

}

Can any look at the JavaScript and suggest anything?

 

 

*This post is locked for comments

I have the same question (0)
  • Andrew Zimmer Profile Picture
    340 on at

    I'm not sure what is going with your code, but it appears to be related to FireOnChange being called from OnLoad.

    I've had good luck by creating a function OnLoad and calling that function wherever I want to update the field visibility. 

     

     Add this code to OnLoad:


    document.HideElement = function(element) {
        if (element == null)
            return;

        element.style.visibility = 'hidden';
    }


    document.HideField = function(field) {
        var hidden = document.getElementById(field);
        if (hidden != null)
            document.HideElement(hidden);

        hidden = document.getElementById(field + "_c");
        if (hidden != null)
            document.HideElement(hidden);

        hidden = document.getElementById(field + "_d");
        if (hidden != null)
            document.HideElement(hidden);
    }


    document.ShowElement = function(element) {
        if (element == null)
            return;

        element.style.visibility = 'visible';
    }

    document.ShowField = function(field) {
        var hidden = document.getElementById(field);
        if (hidden != null)
            document.ShowElement(hidden);

        hidden = document.getElementById(field + "_c");
        if (hidden != null)
            document.ShowElement(hidden);

        hidden = document.getElementById(field + "_d");
        if (hidden != null)
            document.ShowElement(hidden);
    }

     


    document.HandleFieldVisibility = function() {

    if(crmForm.all.new_contacttype.DataValue == 2) 

    {
       crmForm.all.parentcustomerid.DataValue = null;
       crmForm.all.parentcustomerid.ForceSubmit = true;

        document.HideField('parentcustomerid');
        document.ShowField('new_partnerid');
    }
    else if(crmForm.all.new_contacttype.DataValue == 1)  {
       crmForm.all.new_partnerid.DataValue = null;
       crmForm.all.new_partnerid.ForceSubmit = true;

        document.HideField('new_partnerid');
        document.ShowField('parentcustomerid');
    }

    }

    // This actually calls the function
    document.HandleFieldVisibility();

    Add the same line of code to your on change event: 

    document.HandleFieldVisibility();

     

    I hope this helps.  Good luck!

    -Andrew Zimmer 

     

  • Nelson Lopes Profile Picture
    on at

    Andrew, you rock!!!!  This worked perfectly!

     Thank you so much for the help!  I really, really appreciate it

  • Tuka Mauno Profile Picture
    105 on at

    Hi Andrew,

    I didn't try your codes, but could they work if you print the formular? Do the hidden fields disappear?

     

     

  • Andrew Zimmer Profile Picture
    340 on at

    If you are talking about the print preview of the screen, it doesn't hide the fields.  The javascript code is not ran when the print preview screen is displayed.  I hadn't noticed that before. 

    I don't know of a work-around.  The best advice I can give is to put any fields that will always be hidden on the last tab at the bottom so they don't clutter the print preview. 

     If you have fields that are conditionally shown, there isn't much we can do.  You may want to try adding a plugin to prevent the data from being returned.  I the message is either RetrieveMultiple, Retrieve, or Execute.  Doing so will hide the data but the label for the field will still show up. 

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans