Hide a form field

Now that each field offers an OnChange event, we want to use it. At least I do. Based on the selection or values in another field, I'm disabling fields all over the screen. But why disabling and not hide them?! It's not documented and therefore not supported, but it sometimes is needed. The code to hide a field is:


crmForm.all.name_c.style.visibility = 'hidden';
crmForm.all.name_d.style.visibility = 'hidden';

Ofcourse modify the name_c and name_d to the correct fieldname_c and fieldname_d.

Happy hiding!

Update: Thanks to Robert Amos for an even better way to hide the fields.

47 comments:

Anonymous said...

thanks man its informative and helped me

regards
Sharan

Anonymous said...

I added this to the OnChange event and the field is still visible.
What am I doing wrong.

I added this to the Contract field OnChange event:
crmForm.all.name_contract.style.visibility = 'hidden';

Thanks,
Kate

Ronald Lemmen said...

Hi Kate,

What is the schemaname of the field you wish to hide? You can find this in the customization of the entity form. If you go to the third tab, there is the schema name.

If you have this name, then modify the code code as shown here to match your schemaname
crmForm.all.schemaname_c.style.visibility = 'hidden';
crmForm.all.schemaname_d.style.visibility = 'hidden';

Mind the _c and _d of the schema names. They should remain there.

Just let me know if you need more assistance.

Ronald

Jim said...

Ron,
This was a huge help. Combined with the IE developer's toolbar, I'm able to manipulate ton's of visibility. Thanks.

Mujo said...

If you have this name, then modify the code code as shown here to match your schemaname
crmForm.all.schemaname_c.style.visibility = 'hidden';
crmForm.all.schemaname_d.style.visibility = 'hidden';

Mind the _c and _d of the schema names. They should remain there.


Where do these _c and _d's come
from and what do they do?

Ronald Lemmen said...

Those are the names that the td cells have that are being generated for each field. The _c is for the label cell and the _c is for the data of the field cell.

Because this is not in the sdk, it is not supported and upgrades to newer versions might break the upgrade. Just to keep in mind :)

Ronald

Anonymous said...

Hello,
I think this is a great hack. I will be using it all over the place.

I do have a question, I can hide it but I want to show it again if a check box or status has changed.

What is the correct command to SHOW after I have hidden it?

to hide it is 'hidden' what is it to show?

thank you
JB

Ronald Lemmen said...

I'm using the style.display more often now. Then it would be for hiding:

crmForm.all.field.style.display = 'none';

and for showing:
crmForm.all.field.style.display = 'inline';
or
crmForm.all.field.style.display = 'block';

Hope this helps,

Ronald

Mirella said...

Is also possible to hide of display a field based on the value of a lookup field, in my case the customerid field??

Thx. for your help.

Ronald Lemmen said...

Hi Mirella,

You definately can do so. On the lookup field you can place an onchange event. In this event check for its value and then decide to show and/or hide fields. The code will be something like:

if(crmForm.all.primarycontactid.DataValue[0].name == "my name"){
// hide fields
}
else
{
// show fields
}

Also put the same code in the form onload. That will make sure that the initial showing is correct as well.

Kind regards,

Ronald

Anonymous said...

Hi. I'm trying to do this on the opportunity field isrevenuesystemcalculated, using this code:

crmForm.all.isrevenuesystemcalculated_c.visibility = 'hidden';
crmForm.all.isrevenuesystemcalculated_d.visibility = 'hidden';

But it gives me an error saying 'crmForm.all.isrevenuesystemcalculated_c' is null or not an object

Any idea?

Ronald Lemmen said...

Hi,

Make sure that you include .style in between. That makes:
crmForm.all.isrevenuesystemcalculated_c.style.visibility = 'hidden';
crmForm.all.isrevenuesystemcalculated_d.style.visibility = 'hidden';

Hope this helps,

Ronald

Anonymous said...

Hi. Using only crmForm.all.isrevenuesystemcalculated_d.style.visibility = 'hidden'; did the trick. Thanks!

Anonymous said...

Hi,

I'm trying to hide a custom fields in the COntact Form of Administration Tab, but my script is not working:

//Hide fields
crmForm.all.pbs_tempcontactid_c.style.display = 'hidden'; crmForm.all.pbs_tempcontactid_d.style.display = 'hidden';

i wonder what's wrong with it.

thanks

Ronald Lemmen said...

Hi,

It should be either:

crmForm.all.pbs_tempcontactid_d.style.visibility = 'hidden';

or

crmForm.all.pbs_tempcontactid_d.style.display = 'none';

instead of

crmForm.all.pbs_tempcontactid_d.style.display = 'hidden';

hope this helps,

Ronald

laura said...

I added a new button for an incident to the ISV config file and when the record is closed/resolved, i want to hide the button. I was able to retrieve the button name from the id attribute, but when i tried to do the .stlye.visibility = 'hidden' or .style.display = 'none', i am getting an error. How do you hide buttons? thanks!

rifat yavuz said...

can we hide a section ?
with the controls inside, like the panel control... :)

Rafael said...

This code works great but is there a way to hide the field upon opening the entity.

Ronald said...

removing the field earlier is not possible.. it's a shame, but it's like that. maybe a pre-read plugin for titan will allow to do something regarding this..

Anonymous said...

Hi,
This is all very good, and used everywhere, but there is one problem. Anything you hide in javascript (fields, sections, tabs)will always appear if you print the entity.
Any ideas how to get around this?

Anonymous said...

I am having the same issue with the print screen. Hidden fields at the form level appear when users click the Print option.

Has anyone been able to customize the "print" view from a form? Ultimately, I would like to only include a smaller subset of fields from the form.

Ronald Lemmen said...

The print screen is indeed not customizable. I haven't found a way to modify this screen, so I have followed this approach:
- Create a custom aspx page which shows the print preview (based on the fields visible and hidden)
- In the form onload for the custom aspx page add a javascript which prints the current page
- In the form onload for the crm entity screen, search for the print button and modify the onclick event.

Unfortunately I dont have the code at hand, so I can't give it. But you guys can definitely do this yourself.

Hope this helps!

Keri said...

This works wonderfully, thanks so much!

Pete said...

Hi Ronald,

I'm trying to do something similar to this on a form (with CRM 4.0) but I need to hide a textarea. When I set the display styles to "none" the field gets hidden, however the remaining fields to not move up to fill the empty space. This only seems to happen for textarea (ie; multi-line) fields, other fields work fine.
The only solution I have been able to come up with is to place the text area in it's own section and hide the whole thing, but this isn't very tidy. I was wondering if you, or anyone else, has encountered this and could suggest a better alternative.

Best regards,
Pete

Matthew said...

Ronald,
Just wondering as this isn't supported and I have just tried to upgrade to CRM 4 and it doesn't work if you have found a solution for the new version of CRM that will hide the fields in the same type of way.
Thanks,
Matthew.

Graham said...

Hi Ronald,

Many thanks for this guide, has inspired me to attempt a little script myself!

I would like to use your example above of:

if(crmForm.all.primarycontactid.DataValue[0].name == "my name"){
// hide fields
}
else
{
// show fields
}


Could you please explain this part of the code:

DataValue[0].name == "my name")


I have replaced the 'primarycontactid' with 'showfield' which has a yes/no value

I tried using
'DataValue[0].name == "no")'

(i.e. if showfield = no, then hide fields) but this throws the exception that
'crm.Form.all.new_showfield.datavalue.0.name'
is null or not an object


I assume that 'DataValue[0].name == "no")' is the problem? What would this part of the code need to be for a 'bit' value?

Many thanks for any clarity on this in advance!

Graham

Anonymous said...

Hi All,

Is it possible to dynamically hide fields in ms crm 4.0.

I am trying the code bollow, i am getting the code i need in the slert, how do i use it to do the needful.





for (var i; i <3 ; i++)
{

var xfield ="xxx_period"+ i +"initialdate";

var yfield= "xxx_period"+ i+"initialrevenue";

alert(xfield);
alert(yfield);
alert(i);
var a = "crmForm.all.xxx_period"+ i +"initialdate_c.style.visibility = 'hidden';";
var b ="crmForm.all.xxx_period"+ i +"initialdate_d.style.visibility = 'hidden';";
var c = "crmForm.all.xxx_period"+ i +"initialrevenue_c.style.visibility = 'hidden';";
var d = "crmForm.all.xxx_period"+ i +"initialrevenue_d.style.visibility = 'hidden';";
alert(a);
alert(b);
alert(c);
alert(c);

}


Many thanks in advance


Umesh

SkinnyGolfer16 said...

Is it also possible to hide an entire section on a form? I'm sure that it is using a similar technique, I just can't figure out how to get the "name" or id of the section in order to use it in the show/hide code. Any idea how to get that name?

M said...
This comment has been removed by the author.
M said...

To hide a whole section, you need to work backwards from a field in that section:

crmForm.all.new_field.parentElement.
parentElement.parentElement.
style.display = 'none';

Anonymous said...

Hi All,

Is it possible to dynamically hide fields in ms crm 4.0. NOT ENTIRE SECTION.

Say there are 30 fields for periods xxx_period1initialdate to xxx_period30initialdate

if the user chooses 10 i want xxx_period1initialdate to xxx_period10initialdate to be shown.

if 20

xxx_period1initialdate to

xxx_period20initialdate

with tha javascript below i am able to get :


crmForm.all.xxx_period"+ i +"initialdate_c.style.visibility = 'hidden';";

i am able to see the requiered code in the alert.

bust when i just script alert(xfield);

its not hiding the field.

but when i type ( hard code ) its working, is there a dynamic way.







for (var i; i <3 ; i++)
{

var xfield ="xxx_period"+ i +"initialdate";

var yfield= "xxx_period"+ i+"initialrevenue";

alert(xfield);
alert(yfield);
alert(i);
var a = "crmForm.all.xxx_period"+ i +"initialdate_c.style.visibility = 'hidden';";
var b ="crmForm.all.xxx_period"+ i +"initialdate_d.style.visibility = 'hidden';";
var c = "crmForm.all.xxx_period"+ i +"initialrevenue_c.style.visibility = 'hidden';";
var d = "crmForm.all.xxx_period"+ i +"initialrevenue_d.style.visibility = 'hidden';";
alert(a);
alert(b);
alert(c);
alert(c);

}


Many thanks in advance


Umesh

frederic said...

@anonymous

Try :

for(i = 0; i < 10; i++)
{
crmForm.all.item("youRfield" + i + "blabla").style.display = 'none';

or

crmForm.all.item("youRfield" + i + "blabla_c").style.visibility = 'hidden';

&&

crmForm.all.item("youRfield" + i + "blabla_d").style.visibility = 'hidden';

}

Instead of using the _c & _d method, try using the display method..

I haven't test it out .. So let me know if it works for you :)

Anonymous said...

Just an FYI for anyone curious, as I didn't see it mentioned.

If you're using:
crmForm.all.fieldname_c.style.display = 'none';
cmrForm.all.fieldname_d.style.display = 'none';

In order to display the field, use:

crmForm.all.fieldname_c.style.display = 'inline';
crmForm.all.fieldname_d.style.display = 'inline';

Regards,
Jamie

Anonymous said...

Not working comes with error on page.

Patrick said...

btw, it is possible to hide the fields on form load event.

Haniball said...

Hi

Is it possible to hide the "ADD EXISTING ..." button from a Grid in the iFrame?

Thanks for your help and sunny greetings from Switzerland

Anonymous said...

Hi Ronald

Hiding fields and sections works great so far but i would like to be able to expand on it further.
Would it be possible to make fields appear and disappear depending what is chosen in a picklist?
For example:
I have a picklist with 'option_A', 'option_B' & 'option_C' as the choices. Then I have fields 'field_A', 'field_B', 'field_C'.
On loading the page, all the fields should be hidden, but if the user clicks 'option_A' from the picklist, field_A should appear, with B & C hidden. If option b is chosen, field b should appear but A & C should be hidden.

Hope this all makes sense. Any help given would be really appreciated.
Thanks
Phil

frederic said...

Hi Phil : This ain't that difficult to achieve --> I'll give you a small example of how I would do it.. It can be done easier, but i Quickly made an example :

In onload :
HideItem("firstname");
HideItem("lastname");

//Hide all the items your want
function HideItem(sField)
{
if(window.document.getElementById(sField) != null)
{
window.document.getElementById(sField).style.display = "none";
}
}

On Picklist Change :
var picklistValue = crmForm.all.new_picklist2.DataValue;
if(picklistValue == "1")
{
ShowItem("firstname");
HideItem("lastname");
}
else if(picklistValue == "2")
{
ShowItem("lastname");
HideItem("fistname");
}

function HideItem(sField)
{
if(window.document.getElementById(sField) != null)
{
window.document.getElementById(sField).style.display = "none";
}
}

function ShowItem(sField)
{
if(window.document.getElementById(sField) != null)
{
window.document.getElementById(sField).style.display = "inline";
}
}

Anonymous said...

That works great Frederic.
Thanks for your help!!
Phil

Anonymous said...

I have created a Tab in the Lead Form called "Home Details" and would like to hide/display this tab and all the fields on it, depending on yes/no selection (Tick Box)of a field on the "Detail" tab. Thanks Roy

Dave Hawes said...

Hi Ronald! Hope you are well, just replying to Haniball's comment. I've written an article on how to hide that 'Add Existing' button which can be found here:

http://blog.davehawes.com/post/2008/04/24/MSCRM-4-Remove-Add-Existing-xxxxx-button.aspx

Hope that helps!

Cheers,

Dave

jacques said...

I've tried to follow the examples here but haven't quite got it just yet, I can hide a section, or a field but still trying to pull off thexample of hiding a section based on a picklist choice. If somebody could please give me an example. Here is my brief scenario. I have a picklist field called new_type - With the options "new", "existing", "ex"

If you select new - I would just like to section 1,
Existing - Section 1 and Section 2
Ex - Section 1,2 & 3 (All sections)

Thanks so much for the great post.

Regards
Jacques

Ronald Lemmen said...

Hi Jacques,

Look in the examples which are added to the SDK. There is a code sample which is showing how to work with related picklists.

That should help you get your code working.

Kind regards,
Ronald

Anonymous said...

HI Ronald,

My form had hidden field, Is that possible to require fill ONLY when it's show? I means is not require when it's hidden. I got this problem urgently.

Thanks,
ken

Ronald Lemmen said...

Hi Ken,

Next to hiding the field, you will need to change the requirement level of the attribute. I have code samples on the blog for this in CRM 3.0, but this has been changed in 4.0. I'm sure that there is a blog post out there from somebody who will explain you how to change the requirement level of an attribute in CRM 4.0.

Ronald

Revathy said...

hi..all..
thanks for all the valuable informations..really helpful and easily understandable article....
thank you...

Anonymous said...

I am trying to hide fields in a sections based on a checkbox. How can this be done?