RSS

Filtering Lookup depending upon the OptionSet value

23 May

Introduction

We can filter the look up field depending upon the value in OptionSet field using javaScript.

Background

In our case, we have two entities “Account” and “Test”, which have 1:N relationship.
An OptionSet field “new_profession” and a lookup of the “Test” entity are present in “Account” entity.
To retrieve the filtered records depending upon the option selected in the “new_profession” field, we will implement a JScript on the load event of Account entity and OnChange event of the “new_profession” field in Account entity.

Code

function FilteredLookup() {

    var viewId = "{a76b2c46-c28e-4e5e-9ddf-951b71202c9d}"; //Any Guid is fine.
    var entityName = "new_test";// Entity to be filtered
    var profession = Xrm.Page.data.entity.attributes.get("new_profession").getValue();

    if(profession != null && profession != "")
    {
        //Name for the lookup field.
        Xrm.Page.ui.controls.get("new_test_testid").setDisabled(false);

        //Custom Name for the lookup window
        var viewDisplayName = Xrm.Page.data.entity.attributes.get("new_profession").getSelectedOption().text;

        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                          "<entity name='new_test1'>" +
                              "<attribute name='new_name' />" +
                              "<attribute name='new_profession' />" +
                              "<order attribute='new_name' descending='false' />" +
                              "<filter type='and'>" +
                                "<condition attribute='new_profession' operator='eq' value='" + profession + "' />" +
                              "</filter>" +
                          "</entity>" +
                       "</fetch>";

        var layoutXml = "<grid name='resultset' " +
                            "object='1' " +
                            "jump='name' " +
                            "select='1' " +
                            "icon='1' " +
                            "preview='1'>" +
                            "<row name='result' " +
                                "id='new_test1id'>" + // Id attribute of the entity to be filtered
                                "<cell name='new_name' " + //Column 1 – Fullname
                                "width='100' />" +
                                "<cell name='new_profession' " + //Column 2- Profession
                                "width='100' />" +
                            "</row>" +
                        "</grid>";

        //Part3 add new view to the lookup
        Xrm.Page.getControl("new_test1_testid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }
}

By Ranjan Parhi
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

Advertisements
 

Tags: , , , , , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: