Thursday 28 July 2011

Filter Lookup based on Option Set(Picklist) in MS CRM 2011.

Hi All!!!
This is my first blog post.  Here I am going to write about Filter Lookup. How to create filter Lookup based on Option Set (Pick list) in MS CRM 2011.
Let's consider a scenario, we have custom entity called Media where we create different media’s with their different types.  In opportunity product entity there is a Media Type Picklist and Media entity Lookup. So we want to filter Media lookup in Opportunity Product entity based on the Media Type Picklist.
Here is the Code Sample:-

function SetFilterLookup() {
    if (Xrm.Page.getAttribute("new_mediatype").getSelectedOption() != null) {
        var _Text = Xrm.Page.getAttribute("new _mediatype").getSelectedOption().text;
        var _Value = Xrm.Page.getAttribute("new _mediatype").getSelectedOption().value;
        var viewId = "{65EC9B45-EE81-4F89-BAF6-E8603FF8E1E4}";
        var entityName = " new _media";
        var viewDisplayName = "Active Medias for " + _Text;
        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                   "<entity name= new _media'>" +
                   "<attribute name= new _mediatype' />" +
                   "<attribute name= new _mediaid' />" +
                   "<attribute name= new _name' />" +
                   "<order attribute= new _name' descending='false' />" +
                   "<filter type='and'>" +
                   "<condition attribute= new _mediatype' operator='eq' value='" + _Value + "'/>" +
                  "</filter>" +
                   "</entity>" +
                   "</fetch>";

        //build grid layout
        var layoutXml = "<grid name='resultset' " +
                            "object='1' " +
                            "jump= new _name' " +
                             "select='1' " +
                             "icon='1' " +
                             "preview='1'>" +
                         "<row name='result' " +
                          "id= new _mediaid'>" +
                           "<cell name= new _name' " +
                          "width='300' />" +
                          "</row>" +
                       "</grid>";

        //add new view
 Xrm.Page.getControl("new _mediaid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }

}
Call this function on OnChange Event of Media Type Picklist.
Hope it helps you..!!!

No comments:

Post a Comment