Sunday, 15 September 2019

MSCRM 365 v9 bind subgrid using fetch XML by javascript

function QuoteLinesFromQuote(executionContext) { 
    var formContext = executionContext.getFormContext();
    //Show the section on function call.
Xrm.Page.ui.controls.get('Quotelines').setVisible(true);
    // Xrm.Page.ui.tabs.get("tab_Quotes").sections.get("CONTACT_PREFERENCES").setVisible(true);
    // Quotelines : is name of subgrid given on Form. 
    var objSubGrid = window.parent.document.getElementById("Quotelines");
    var globalContext = Xrm.Utility.getGlobalContext();
    var version = globalContext.getVersion();
    // You need the timeout for at least the form is properly loaded.
    var subRowCount = 1;
    if (subRowCount == 0) {
        setTimeout(QuoteLinesFromQuote(executionContext), 5000);
    } else {
        // When subgrid is loaded, get the other relevant value.
        var QuoteIDValue = formContext.data.entity.getId();
        //Create FetchXML for sub grid to filter records
        var FetchXml = '<fetch version = "1.0" output-format="xml-platform" mapping = "logical" distinct = "false" > ' +
            '<entity name="quotedetail">' +
            '<attribute name="productid" />' +
            '<attribute name="productdescription" />' +
            '<attribute name="priceperunit" />' +
            '<attribute name="quantity" />' +
            '<attribute name="extendedamount" />' +
            '<attribute name="quotedetailid" />' +
            '<order attribute="productid" descending="false" />' +
            '<filter type="and">' +
            '<condition attribute="quoteid" operator="eq" uitype="quote" value="{' + QuoteIDValue + '}" />' +
            '</filter>' +
            '</entity>' +
            '</fetch>';

        // Layout of subgrid.
        //var LayoutXml = "<grid name='resultset' object='8' jump='new_name' select='1' preview='1' icon='1'>" +
        //    " <row name='result' id='new_boat'>" +
        //    "<cell name='productid' width='100' />" +
        //    "<cell name='productdescription' width='200' />" +
        //    "<cell name='priceperunit' width='100' />" +
        //    "<cell name='quantity' width='200' />" +
        //    "<cell name='quotedetailid' width='200' />" +
        //    "</row>" +
        //    "</grid>";
        // Apply layout and filtered fetchXML
        //objSubGrid.control.SetParameter("layoutXml", LayoutXml);
        objSubGrid.control.SetParameter("fetchXml", FetchXml);
        //Refresh grid to show filtered records only.
        objSubGrid.control.Refresh();
    }
}

No comments:

Post a Comment