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) {
           
 fetchXml = "<filter type='and'><condition attribute='new_regioncode' operator='eq' value='" + regioncode + "' /></filter>";  

            Xrm.Page.getControl("new_state").addCustomFilter(fetchXml);
        }
    } catch (e) {
        ShowCatchMessage(e.message);
    }
}

Output:

Comments

  1. 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.

    ReplyDelete
  2. How about added order ascending by state ? Can you write in to fetchxml ?

    ReplyDelete
  3. Code doesn't work

    ReplyDelete
  4. Sorry, it works like a charm

    ReplyDelete

Post a Comment

Popular posts from this blog

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

go to top image