Dynamics CRM 4.0 · Javascript

CRM 4.0 Hide Picklist items with JavaScript Automatically

Many of us have encountered a situation where we have to dynamically hide Picklist items in CRM form. There are many reasons for doing this, such as dynamic business rules, unused picklist item but still want to keep the data.

Hiding the picklist item with Javascript is pretty simple:


picklist_field.options.remove(index); //index is zero based

And you will simply copy and paste this code for all the items you want to hide.

Now, what if we change the picklist item description (add it with “DO NOT USE” text) so that users know not to use these items. Well, that’s good and all, but the picklist will still have those unnecessary items just crowding and tainting the screen.

Well, what if we have a Javascript code that will hide all items with “DO NOT USE” text but still show if it has been selected in existing records.


function HidePicklistItemContainingText(fieldId, hideContainText)

{

var picklist = document.getElementById(fieldId);

if (hideContainText != null && hideContainText.length > 0)

{

for (var i=picklist.options.length - 1; i>=0; i--)

{

if (picklist.options[i].text.indexOf(hideContainText) != -1 && picklist.selectedIndex != i)

picklist.options.remove(i);

}

}

}

To use it, simply copy and paste to CRM form Javascript event code and call the method HidePicklistItemContainingText((fieldId, hideContainText).


HidePicklistItemContainingText(new_picklistfield, "DO NOT USE");

Cheers – Sy

Advertisements

2 thoughts on “CRM 4.0 Hide Picklist items with JavaScript Automatically

  1. hi, I am little confused by this piece of code..what data needs to be passed for new_picklistfield, or fieldId? I tried the fieldname and it errors out all the time. thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

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