Dependent and Dynamic Picklist / Option Sets

If you want a JavaScript solution for having dependent picklists without making them their own entities and making them lookups the following will get you there.

Here is a bit of code to add as many Dependent picklists as you want with a chunk of re-usable code.  There is a Parent and Child picklist.  All you have to do is setup the JS and keep the Text of the Picklist in the following format:

Parent Example (new_parentPickList):
Option 1 [1]
Option 2 [2]
Option 3 [3]

Child Example (new_childPicklist):
1 | Child Option A
1 | Child Option B
1 | Child Option C
2 | Child Option A
3 | Child Option A
3 | Child Option B
xx Old Child Option A
xx Old Child Option B

Note:  To hide older child options without deleting them just add ‘xx’ to the start of the Picklist text.

Add the following JS Code:
Url to code: http://www.textsnip.com/plto6o/java

JS Code:



function dynamicDropdown(parent, child)
{
	filterPicklist(parent, child);		
}

function parentListFilter(parent, id)
{
	var filter = "";
		if (getParentCode(parent) != "")
		{
			filter = getParentCode(parent);
		}
		else
		{
                       // No [ ] match
		}

	return filter;
}

function filterPicklist(parent, child)
{
	var parentList = Xrm.Page.getAttribute(parent).getValue();
	var childListControlAttrib = Xrm.Page.getAttribute(child);
	var childListOptions = childListControlAttrib.getOptions();
	var childListControl = Xrm.Page.getControl(child);
	var codeToFilterListOn = parentListFilter(parent, parentList);
	
	
	if (codeToFilterListOn != "")
	{
		childListControl.clearOptions();
		for (var optionIndex in childListOptions)
		{
			var option = childListOptions[optionIndex];
			// Ignore xx and check for Match
			if (option.text.substring(0, 2) != "xx" && option.text.indexOf(codeToFilterListOn) > -1)
			{
				childListControl.addOption(option);
			}
		}
	}
	else
	{
		// Didn't match, show all?
	}
}

function getParentCode(parent)
{
	//Get Parent Code Dynamically from inside [ ]
	var filter = "";
	var parentValue = Xrm.Page.getAttribute(parent).getText();
	if (parentValue && parentValue.indexOf("]") > -1)
	{
		var parentCode = parentValue.substring(parentValue.indexOf("[") + 1, parentValue.indexOf("]"));
		if (parentCode)
		{
			filter = parentCode + " | ";
		}
		else
		{}
	}
	return filter;
}

Then add the following Event Handler on the On Change event for the Parent Picklist Control:

dropdowns

Leave a Reply

Your email address will not be published. Required fields are marked *