Dependent OptionSet in Dynamics CRM V9
Hi companions, In microsoft dynamics crm one of the significant job is dependent Optionset. Here in alternative set Microsoft crm take Someaddon is Dependent OptionSet. We as a whole know fundamentally how to utilize choice set in Microsoft CRM.
In any case, I confronted one requirment in before somedays that is reliant choice set. All things considered the prerequisite depends on parent alternative I have to show related parent sub choices. For instance we have numerous states in India here state is parent choice in that state they have numerous locale, here region is sub choices in parent choice.
At the point when I chose parent choice is an "Andhrapradesh" Then consequently they show all related region in Andhrapradesh. For this situation we had just one way is utilizing subordinate alternative set by utilizing of XML or JSON to split the prerequisite. In Account element I made two fields
1) state
2) District
Here state alternative set I would take two expresses that is "Andhrapradesh" Option set worth is "100000000" and one more state is "Telangana" Option set worth is "100000001".In Andhrapradesh I need to take 3 " District "
*) "Kadapa" Option set worth is "1".
*) " kurnool" Option set worth is "2".
*) " East Godavari" Option set worth is "3".
In Telangana I need to take 2 " District "
*) "Hyderabad" Option set worth is "5".
*) " Medak" Option set worth is "6".
On the off chance that I chose the alternative Andhrapradesh I have to show just kadapa, kurnool and East Godavari .
Before we began the cycle we have to download the "SDK. DependentoOptionSet. Js" We don't have to stress over how to and were it is accessible for download. Of course SDK gives the Js document.
SDK==>SampleCode==>Js==>FormScript==>SDK. DependentoOptionSet. Js.
Make a JavaScript Webresource name as a "SDK. DependentoOptionSet. Js".
SDK Code:
/If the SDK namespace object isn't characterized, make it.
in the event that (typeof (SDK) == "vague")
{ SDK = {}; }
/Create Namespace compartment for capacities in this library;
SDK.DependentOptionSet = {};
SDK.DependentOptionSet.config = invalid;
/**
* @function SDK.DependentOptionSet.init
* @param {string} webResourceName the name of the JavaScript web asset containing the JSON definition
* of alternative conditions
*/
SDK.DependentOptionSet.init = work (webResourceName) {
on the off chance that (SDK.DependentOptionSet.config == invalid) {
/Retrieve the JavaScript Web Resource indicated by the boundary passed
var clientURL = Xrm.Page.context.getClientUrl();
var pathToWR = clientURL + "/WebResources/" + webResourceName;
var xhr = new XMLHttpRequest();
xhr.open("GET", pathToWR, valid);
xhr.onreadystatechange = work () {
on the off chance that (this.readyState == 4/* complete */) {
this.onreadystatechange = invalid;
on the off chance that (this.status == 200) {
SDK.DependentOptionSet.config = JSON.parse(this.response);
SDK.DependentOptionSet.completeInitialization();
}
else {
toss new Error("Failed to stack setup information for subordinate alternative sets.");
}
}
};
xhr.send();
}
else {
SDK.DependentOptionSet.completeInitialization();
}
};
/**
* @function SDK.DependentOptionSet.completeInitialization
* Initializes the needy alternative set choices when the structure loads
*/
SDK.DependentOptionSet.completeInitialization = work () {
/If the parent field is invalid, ensure the youngster field is invalid and incapacitated
/Otherwise, call fireOnChange to channel the kid choices
for (var I = 0; I < SDK.DependentOptionSet.config.length; i++) {
var parentAttribute = Xrm.Page.getAttribute(SDK.DependentOptionSet.config[i].parent);
var parentFieldValue = parentAttribute.getValue();
in the event that (parentFieldValue == invalid || parentFieldValue == - 1) {
var childAttribute = Xrm.Page.getAttribute(SDK.DependentOptionSet.config[i].child);
childAttribute.setValue(null);
childAttribute.controls.forEach(function (c) { c.setDisabled(true); });
}
else {
parentAttribute.fireOnChange();
}
}
}
/**
* @function SDK.DependentOptionSet.filterDependentField
* Locates the right arrangement of designs
* @param {string} parentFieldParam The name of the parent field
* @param {string} childFieldParam The name of the reliant field
*/
SDK.DependentOptionSet.filterDependentField = work (parentFieldParam, childFieldParam) {
debugger;
/Looping through the variety of all the conceivable reliance designs
for (var I = 0; I < SDK.DependentOptionSet.config.length; i++) {
var dependentOptionSet = SDK.DependentOptionSet.config[i];
/* Match the boundaries to the right needy optionset mapping*/
on the off chance that ((dependentOptionSet.parent == parentFieldParam) &&
(dependentOptionSet.child == childFieldParam)) {
/*
* Using setTimeout to permit a brief period between calling this conceivably recursive capacity.
* Without including some time between calls, the incentive toward the finish of the chain of conditions
* was being set to invalid on structure load.
*/
setTimeout(SDK.DependentOptionSet.filterOptions,
100, parentFieldParam,
childFieldParam,
dependentOptionSet);
}
}
};
/**
* @function SDK.DependentOptionSet.filterOptions
* Filters choices accessible in subordinate fields when the parent field changes
* @param {string} parentFieldParam The name of the parent field
* @param {string} childFieldParam The name of the needy field
* @param {object} dependentOptionSet The design information for the reliant choices
*/
SDK.DependentOptionSet.filterOptions = work (parentFieldParam, childFieldParam, dependentOptionSet) {
/* Get references to the related fields*/
var parentField = Xrm.Page.getAttribute(parentFieldParam);
var parentFieldValue = parentField.getValue();
var childField = Xrm.Page.getAttribute(childFieldParam);
/* Capture the current estimation of the youngster field*/
var currentChildFieldValue = childField.getValue();
/* If the parent field is invalid, set the Child field to invalid */
/Interactive Service Hub, CRM for Tablets and CRM for telephones can return - 1 when no choice chose
in the event that (parentFieldValue == invalid || parentFieldValue == - 1) {
childField.setValue(null);
childField.fireOnChange();/channel any ward optionsets
/Any characteristic may have quite a few controls
/So incapacitate each example
childField.controls.forEach(function (c) {
c.setDisabled(true);
});
/Nothing more to do when parent characteristic is invalid
return;
}
/The substantial kid choices characterized by the design
var validOptionValues = dependentOptionSet.options[parentFieldValue.toString()];
/When the parent field has a worth
/Any property may have more than one control in the structure,
/So repeat over every one
childField.controls.forEach(function (c) {
c.setDisabled(false);
c.clearOptions();
/The property contains the legitimate choices
var childFieldAttribute = c.getAttribute();
/The property alternatives for the Interactive Service Hub, CRM for Tablets and
/CRM for telephones customers do exclude a definition for an unselected choice.
/This will include it
in the event that (Xrm.Page.context.client.getClient() == "Portable") {
c.addOption({ text: "", esteem: - 1 });
}
/For every alternative worth, get the definition from the ascribe and add it to the control.
validOptionValues.forEach(function (optionValue) {
/Get the alternative defnition from the quality
var alternative = childFieldAttribute.getOption(parseInt(optionValue));
/Add the alternative to the control
c.addOption(option);
})
});
/Set the incentive back to the current worth in the event that it is a substantial worth.
in the event that (currentChildFieldValue != invalid &&
validOptionValues.indexOf(currentChildFieldValue.toString()) > - 1) {
childField.setValue(currentChildFieldValue);
}
else {
/Otherwise set it to invalid
childField.setValue(null);
childField.fireOnChange();/channel some other ward optionsets
}
}
Make another Webresource name as "new_dependentoOptionSet.json".
Json Code:
[
{
"parent": "new_state",
"Chaild": "new_district",
"Options": {
"100000000": [
"1",
"2",
"3"
],
"100000001": [
"5",
"6"
]
}
}
]
Pick "OnChange" occasion and give the beneath function name is
SDK.DependentOptionSet.filterDependentField
Thats it save and publish.
Thank you...!
1 Comments
Hello, could you tell me how could we use this code with Multiple Option Set ?
ReplyDeleteI tried to apply it with a multiple one but on the Onload all values from Multiple OptionSet are always deleted. Do you have a workarround?