locked
How to bind subgrid record dynamically based on the form Lookup value changes RRS feed

  • Question

  • Hi all,

          How to bind subgrid recors dynamically based on the form Lookup value Changes

    Friday, November 15, 2013 9:44 AM

All replies

  • Hi,

    You could refresh sub-grid in lookup onchange event

    gridControl = Xrm.Page.ui.controls.get("yourSubGridName");
    gridControl.refresh();

    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    Friday, November 15, 2013 9:49 AM
  • hi Vikram,

               i Need to load data based on another lookup.

    (Eg).. if i select 2345 in lookup1 means Subgrid need to show only the records with 2345 only.

    Friday, November 15, 2013 9:52 AM
  • Hi,

    Please check following link

    http://srmscrm.wordpress.com/2012/11/05/changing-the-subgrid-fetchxml-in-ms-crm-2011/


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    Friday, November 15, 2013 9:56 AM
  • Hi

    To bind subgrid based on lookup change you can write fetchxml query in javascript.

        

        var grid = document.getElementById('subgridname');
        if (grid == null) {
            setTimeout('updateSubGrid()', 2000);
        }
        else {

            var lookupfield = new Array;

            lookupfield = Xrm.Page.getAttribute("lookupid").getValue();

            if (lookupfield != null) {

                var lookupid = lookupfield[0].id;
            }
            else {
                return;
            }
            var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
            fetchXml += "<entity name='entityname'>";
            fetchXml += "<attribute name='anyselected' />";       
            fetchXml += "<filter type='and'>";
            fetchXml += "<condition attribute='filterid' operator='eq' value='" + lookupid + "' />";
            fetchXml += "</filter>";  
            fetchXml += "</entity>";
            fetchXml += "</fetch>";

            grid.control.setParameter("fetchXml", fetchXml);      

            grid.control.refresh();
        }

    Suresh Sorde

    Friday, November 15, 2013 10:23 AM
  • Hi Jerry,

    Please look at this code which might be useful for your query. I have worked on Case entity.  As per your requirement change the XML code

    function updateSubGrid() {  
        var relatedProducts = document.getElementById("OrderProducts");   //replace name of the subgrid
        var lookupfield = new Array;
        lookupfield = Xrm.Page.getAttribute("new_orderid").getValue();  //replace name of the lookup field
        if (lookupfield != null) {
            var lookupid = lookupfield[0].id;
        }
        //Else the function will return and no code will be executed.
       else {
            return;
        }
        if (relatedProducts ==null || relatedProducts.readyState != "complete")
        {
          //This statement is used to wait for 2 seconds and recall the function until the grid is loaded.
            setTimeout('updateSubGrid()', 2000);
            return;
        }    

        //This is the fetch xml code which will retrieve all the order products related to the order selected for the case. As per your requirement change the XML code

        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
        fetchXml += "<entity name='salesorderdetail'>";
        fetchXml += "<attribute name='productid' />";
        fetchXml += "<attribute name='productdescription' />";
        fetchXml += "<attribute name='priceperunit' />";
        fetchXml += "<attribute name='quantity' />";
        fetchXml += "<attribute name='extendedamount' />";
        fetchXml += "<attribute name='salesorderdetailid' />";
        fetchXml += "<order attribute='productid' descending='false' />";
        fetchXml += "<link-entity name='salesorder' from='salesorderid' to='salesorderid' alias='aa'>";
        fetchXml += "<filter type='and'>";
        fetchXml += "<condition attribute='salesorderid' operator='eq' uiname='Order Name' uitype='salesorder' value='" + lookupid + "' />";
        fetchXml += "</filter>";
        fetchXml += "</link-entity>";
        fetchXml += "</entity>";
        fetchXml += "</fetch>";
        relatedProducts.control.setParameter("fetchXml", fetchXml);

        relatedProducts.control.refresh(); 

    }   


    Prudveen D If the answer helped you, remember to mark it as answer.

    • Proposed as answer by Prudween Friday, November 15, 2013 11:04 AM
    Friday, November 15, 2013 11:04 AM