Wednesday, 2 October 2013

Dynamics CRM 2011 - Filter a lookup

Filtering a lookup in CRM 2011 is quite straight forward. Below is a sample that I used to filter one lookup based on the value of another.



function filterSecondLookup()
{
    try {
        var lookupObject = Xrm.Page.getAttribute("lookup_one").getValue();

        if (lookupObject != null)
        {

            lookup_one_name = $('').text(lookupObject[0].name).html();  // text of lookup
            lookup_one_id = lookupObject[0].id; // Guid of lookup

            var viewId = '{1ACB2B35-B07C-44D1-868D-258DEEAB88E2}'; //Random GUID
            var entityName = 'account';
            var viewDisplayName = 'Filtered Lookup Name';

            var fetchXml = "" +
                              "" +
                                "" +
                                "" +
                                "" +
                                "" +
                                "" +
                                "" +
                                "" +
                                  "" +
                                    "" +
                                  "" +
                                "" +
                              "" +
                            "";

            var layoutXml = "" +
                          "" +
                            "" +
                            "" +
                            "" +
                            "" +
                          "" +
                        "";

            Xrm.Page.getControl('lookup_two').addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
        }
        else
        {
            Xrm.Page.getControl("lookup_two").setDefaultView(window.defaultSiteViewId );
        }
    }
    catch (ex)
    {
        alert("Error in filterSecondLookup: " + ex);
    }
}

No comments:

Post a Comment