Monday, July 18, 2011

CRM 2011 - Add Custom View To SubGrid Using Java Script

Method
function addSubgridCustomView(subgridID, entityTypeCode, displayName, fetchXML, layoutXML, filterType, isDefault) {

    if (IsNull(subgridID)) { throw Error.argument("value", "ID of sub-grid to filter not provided"); return; }
    if (IsNull(entityTypeCode)) { throw Error.argument("value", "Entype code of returned object by fetchXML not provided"); return; }
    if (IsNull(fetchXML)) { throw Error.argument("value", "FetchXml not provide for custom view on sub-grid"); return; }
    if (IsNull(layoutXML)) { throw Error.argument("value", "LayoutXml not provided for custom view on sub-grid"); return; }

    var grd = document.getElementById(subgridID);

    //Check if the sub-grid has any customViews already. 
    var customViews = (IsNull(grd.customViews) ? new Array() : grd.customViews);


    var viewId = "{65EC9B45-EE81-4F89-BAF6-E8603FF8E1E9}";

    //Create an object to hold the customView's information 
    var customView = new Object();
    customView.fetchXml = fetchXML;
    customView.id = viewId;
    customView.layoutXml = layoutXML;
    customView.name = (IsNull(displayName) || displayName == "" ? "Filtered Lookup" : displayName);
    customView.recordType = entityTypeCode;
    customView.Type = (!IsNull(filterType) ? filterType : 0);

    //Add the customView object to the array of customViews 
    customViews.push(customView);

    //Add the array of custom views to the sub-grid 
    grd.customViews = customViews;

    //Set this view as the default if desired 
    if (isDefault) { grd.defaultViewId = viewId; }
Call
function SetProductCategoryView() {
    formType = Xrm.Page.ui.getFormType();
    if (formType == FORM_TYPE_UPDATE) {
        var viewId = "{65EC9B45-EE81-4F89-BAF6-E8603FF8E1E9}";
        var memberID = "";
        var entityName = "fia_memberproductcategory";
        var viewDisplayName = "Product Categories";
        var fetchXml = "<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">" +

                   "<entity name="fia_memberproductcategory">" +

                   "<attribute name="fia_memberproductcategoryid">" +

                   "<attribute name="fia_name">" +

                   "<order attribute="fia_name" descending="false">" +
                     "<link-entity  ="" from="fia_MemberProductCategoryId" link-type="inner" name="fia_memberproductcategory" to="fia_memberproductcategoryId"> " +

                  "<link-entity  ="" from="fia_memberproductid" link-type="inner" name="fia_fia_memberproduct_account" to="fia_memberproductid"> " +
                               "           <filter type="and">" +
                       "         <condition attribute="accountid" operator="eq" value="E03E2524-CDA6-E011-9F05-BC50FDF3E385">" +
                       "      </condition></filter>   " +
                        "     </link-entity>" +

                        "     </link-entity>" +
                   "</order></attribute></attribute></entity>" +
                   "</fetch>";



        //build grid layout
        var layoutXml = "<grid "="" +<br="" name="resultset">                             "object='1' " +
                             "jump='fia_memberproductcategory' " +
                             "select='1' " +
                             "icon='1' " +

                             "preview='1'&>" +

                         "<row "="" +<br="" name="result">
                              "id='fia_name'&>" +

                           "<cell "="" +<br="" name="fia_name">
                                 "width='300' /&>" +



                         "</cell></row>" +

                       "</grid>";

     

        //add new view view
        addSubgridCustomView("ProductCategory", getObjectTypeCode(entityName), viewDisplayName, fetchXml, layoutXml,0, true);
    }
}

function getObjectTypeCode(entityName) {

    try {

        var lookupService = new RemoteCommand("LookupService", "RetrieveTypeCode");
        lookupService.SetParameter("entityName", entityName);
        var result = lookupService.Execute();

        if (result.Success && typeof result.ReturnValue == "number") {
            alert(result.ReturnValue);
            return result.ReturnValue;
        } else {
            return null;
        }
    }
    catch (ex) {
        throw ex;
    }
}

3 comments:

  1. getting error as 'Xrm is undefined'. Please help

    ReplyDelete
  2. Hi Thanks for the post,

    After exceuting the above code through javascript i am not able to see any views created on subgrid?

    Can you tell is there any way to pass the fetchxml to subgrid views

    ReplyDelete