Working with cascading Picklist/OptionSet in JS

One day I’ve got an interesting requirement. Generate cascading OptionSets (a few levels down) for products selection, in order to guide the sales staff in selecting the proper product as it relates to the rest of the products on the order. Interesting!

So, I’ve started with adding a few extra classification fields to the Product entity, to describe the relations. Then you add the relationships as needed.

On the client side, I’m doing the filtering using JS.

First off, in order to guide the user in making the selection, I only make visible the top drop-down, and hide the rest.

I’m using the following lines of code to make the drop-downs visible/hidden:

Xrm.Page.getControl("new_fieldname").setVisible(false);

Xrm.Page.getControl("new_fieldname").setVisible(true);

Next, I get the reference to the drop-down as such:

var dd1 = Xrm.Page.ui.controls.get("new_fieldname");

and I also want to get the values:

var options = dd1.getAttribute().getOptions();

Getting the Text and the Value properties of the drop-down I’m using a for loop, and reading each option:

var picklstvalue = options[i].value;
var picklsttext = options[i].text;

and then I can just remove the options I don’t want to display:

dd1.removeOption(picklstvalue);

Other useful methods  for this case are addOption, getDisabled, getName, setFocus.

See all methods with description and samples on MSDN at: http://msdn.microsoft.com/en-us/library/gg334266.aspx

Enjoy!

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

A WordPress.com Website.

Up ↑

%d bloggers like this: