How to add custom filter lookup field based on OptionSet field selection in Dynamics CRM.
In this blog I am going to show how we can filter records based on the selected option set value.
For this example I have created an entity and placed 2 fields mainly, One is Option set Field for Region/ Divison and another is Lookup filed of State. So when user will select the region the lookup values will show only filtered records based on the region, copy and paste the code below in your form option set OnChange event which will filter record of lookup.
function FilterPackagesLookup() {
try {
preFilterLookup();
} catch (e) {
ShowCatchMessage(e.message);
}
}
function preFilterLookup() {
try {
if (Xrm.Page.ui.controls.get("new_state") != "undefined" && Xrm.Page.ui.controls.get("new_state") != null) {
Xrm.Page.getControl("new_state").addPreSearch(function () {
addLookupFilter();
});
}
} catch (e) {
ShowCatchMessage(e.message);
}
}
function addLookupFilter() {
try {
var regioncode = Xrm.Page.getAttribute("new_regioncode").getValue();
if (regioncode != null) {
For this example I have created an entity and placed 2 fields mainly, One is Option set Field for Region/ Divison and another is Lookup filed of State. So when user will select the region the lookup values will show only filtered records based on the region, copy and paste the code below in your form option set OnChange event which will filter record of lookup.
function FilterPackagesLookup() {
try {
preFilterLookup();
} catch (e) {
ShowCatchMessage(e.message);
}
}
function preFilterLookup() {
try {
if (Xrm.Page.ui.controls.get("new_state") != "undefined" && Xrm.Page.ui.controls.get("new_state") != null) {
Xrm.Page.getControl("new_state").addPreSearch(function () {
addLookupFilter();
});
}
} catch (e) {
ShowCatchMessage(e.message);
}
}
function addLookupFilter() {
try {
var regioncode = Xrm.Page.getAttribute("new_regioncode").getValue();
if (regioncode != null) {
fetchXml = "<filter type='and'><condition attribute='new_regioncode' operator='eq' value='" + regioncode + "' /></filter>";
}
} catch (e) {
ShowCatchMessage(e.message);
}
}
I have applied Autocomplete on phone. .How can I filter values based on selected value.For Ex:I have created two fields "country and states" I want to see state based on country value.
ReplyDeleteHow about added order ascending by state ? Can you write in to fetchxml ?
ReplyDeleteCode doesn't work
ReplyDeleteSorry, it works like a charm
ReplyDeleteis the same option available in D365 as well?
ReplyDeleteohh man.. it's working.. tysm
ReplyDelete