ttajic
2/9/2017 - 11:53 AM

OptionSet Filtering

OptionSet Filtering

/* author: Rok Ljajič */

var Adacta = Adacta || {};
Adacta.IndustrySubCategory = [];

/*
Required fields:
  - ad_industry <- optionset (global)
  - ad_industrysubcategory <- optionset (global)
  - ad_industryother <- string
*/
/*
 - IndustryOnLoad <- Form OnLoad
 - OnIndustryChange <- Set On ad_industry on change event
 - OnSubIndustryChange <- Set on ad_industrysubcategory on change event
*/
function IndustryOnLoad()
{
	// save all values from option set to new variable (we'll have to filter it, so we need old values if anything changes)
	var industrySub = Xrm.Page.getControl("ad_industrysubcategory");
	Adacta.IndustrySubCategory = industrySub.getOptions();
	
	OnIndustryChange();
}

function SetIndustrySubCategoryValues(allowedValues)
{
	var subIndustry = Xrm.Page.getControl("ad_industrysubcategory");
	if(!subIndustry) {
		return;
	}
	
	subIndustry.setDisabled(false);
	
	var subIndustryAtt = Xrm.Page.getAttribute("ad_industrysubcategory");
	// clear subCategory and call fire on change, so any function which is executed on change, will be called
	if(allowedValues.indexOf(subIndustryAtt.getValue()) == -1) {
		subIndustryAtt.setValue(null);
		subIndustryAtt.fireOnChange();
	}
	
	// retrieve all options
	var options = subIndustry.getOptions();
	
	// clear current options from sub category option set
	for(var i=0; i < options.length; i++) {
		subIndustry.removeOption(options[i].value);
	}
	
	// go trough all original options from sub category option set and check each option is exists in allowedValues array.
	// if it does, add it to option
	for(var i=0; i < Adacta.IndustrySubCategory.length; i++) {
	
		if(allowedValues.indexOf(Adacta.IndustrySubCategory[i].value) > -1) {
			subIndustry.addOption(Adacta.IndustrySubCategory[i]);
		}					
	}
	
	// if we have only one option (other), then set it as default value
	if(allowedValues.length == 1) {
		subIndustryAtt.setValue(allowedValues[0]);
		subIndustryAtt.fireOnChange();
	}
}

// on change, clear sub category option set
function OnIndustryChange()
{
	var industry = Xrm.Page.getAttribute("ad_industry");
	if(!industry) {
		return;
	}
	
	// list of all available statuses for each main category option
	var industryStatuses =
	[
		[899270000, 899270001, 899270002, 899270003, 899270008],
		[899270004, 899270005, 899270006, 899270008],
		[899270007, 899270008]
	];
	
	var industryValue = industry.getValue();
	
	// call function with available statuses for selected status
	switch(industryValue) {
		case 899270000:	SetIndustrySubCategoryValues(industryStatuses[0]);
			break;
		case 899270001:	SetIndustrySubCategoryValues(industryStatuses[1]);
			break;
		case 899270002:	SetIndustrySubCategoryValues(industryStatuses[2]);
			break;
		case 899270003:	EnableDisableIndustrySubCategory();
			break;
	}
}

// if main category is changed to 899270008 (other), we don't need sub category, so we can disable sub category option set
function EnableDisableIndustrySubCategory()
{
	var subIndustry = Xrm.Page.getControl("ad_industrysubcategory");
	var subIndustryAtt = Xrm.Page.getAttribute("ad_industrysubcategory");
	if(!subIndustry) {
		return;
	}
	
	subIndustry.setDisabled(true);
	subIndustryAtt.setValue(null);
	
	var industryOther = Xrm.Page.getControl("ad_industryother");
	if(!industryOther) {
		return;
	}
	
	industryOther.setDisabled(false);
}

// on sub category change, set other field as enabled if selected value is other, otherwise clear field other and disable it
function OnSubIndustryChange()
{
	var subIndustry = Xrm.Page.getAttribute("ad_industrysubcategory");
	if(!subIndustry) {
		return;
	}
	
	var industryOther = Xrm.Page.getControl("ad_industryother");
	var industryOtherAtt = Xrm.Page.getAttribute("ad_industryother");
	if(!industryOther) {
		return;
	}
	
	if(subIndustry.getValue() == 899270008) {
		industryOther.setDisabled(false);
	} else {
		industryOther.setDisabled(true);
		industryOtherAtt.setValue(null);
	}	
}