Saturday 30 July 2011

Filter Custom Unit of measurement (Unit) Lookup with multiple conditions

Please read simple filter lookup in ms crm2011, my last post before implement this.
Let’s take a simple scenario, we have custom unit lookup in opportunity product entity. We want to filter Custom Unit Lookup based on different product’s set to Custom Media entity where Media and Product entity have many-to-many relationship.
1.       Use link-entity in FetchXML

Here is the code sample:-

function SetUnitFilterLookup() {
                if (Xrm.Page.getAttribute("new_mediaid").getValue() != null) {
                var _Value = Xrm.Page.getAttribute("new_mediaid").getValue()[0].id;
                var viewId = "{65EC9B45-EE81-4F89-BAF6-E8603FF8E1E4}";
                var entityName = "uom";
                var viewDisplayName = "Active Units For Media"
                var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical'      distinct='true'>" +
                  "<entity name='uom'>" +
                   "<attribute name='uomid'/>" +
                   "<attribute name='name'/>" +
                   "<link-entity name='product' to='uomid' from='defaultuomid' visible='false' intersect='true'>" +
                   "<link-entity name=new_new_media_product' to='productid' from='productid' visible='false' intersect='true'>" +
                   "<link-entity name=new_media' alias='ab' to=new_mediaid' from=new_mediaid' visible='false' intersect='true'>" +
                   "<filter type='and'>" +
                   "<condition attribute=new_mediaid' value='" + _Value + "' operator='eq'/>" +
                   "</filter></link-entity></link-entity></link-entity></entity></fetch>";

        //build grid layout

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

        //add new view view

        Xrm.Page.getControl("new_unitofsaleid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }
}

Call this function on OnChange Event of Media Lookup field.
Hope it helps someone!!!

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