Search This Blog

Wednesday, January 29, 2014

Update SubGrid Control at Runtime on Entity Form

Updating Subgrid control on the MS CRM Form.
below example demonstrate the Subgrid on custom entity which will filter Contact in contact sub grid , on search of specific firstname and lastname.

 


function UpdateSubGrid()
        {

            var subgridcontact = window.parent.document.getElementById("contact_subgrid");
            var subgridlead = window.parent.document.getElementById("lead_subgrid");

            var fname = window.parent.Xrm.Page.getAttribute("new_firstname").getValue();
            var lname = window.parent.Xrm.Page.getAttribute("new_lastname").getValue();
            var phonenumber = window.parent.Xrm.Page.getAttribute("new_phonenumber").getValue();

            //If this method is called from the form OnLoad, make sure that the grid is loaded before   proceeding

            if (subgridcontact == null || subgridcontact.readyState != "complete")
             {
                 setTimeout('UpdateSubGrid()', 1000);
                 return;
            }
            if (subgridlead == null || subgridlead.readyState != "complete")
            {
                setTimeout('UpdateSubGrid()', 1000);
                return;
            }

            //Update the fetchXML that will be used by the grid - Search in Contact.
     

            var fetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>";
            fetchXml += "<entity name='contact'> <attribute name='firstname'/> <attribute name='lastname'/> <attribute name='mobilephone'/>";
            fetchXml += "<order descending='false' attribute='firstname'/>";
            fetchXml += "<filter type='and'>";
            if (fname != null)
            {
                fetchXml += "<condition attribute='firstname' value='" + fname + "%' operator='like'/>";
             
            }
            if (lname != null)
            {
                fetchXml += "<condition attribute='lastname' value='" + lname + "%' operator='like'/>";
            }
            if (phonenumber != null)
            {
                fetchXml += "<condition attribute='mobilephone' value='" + phonenumber + "%' operator='like'/>";
            }
            fetchXml += "</filter>";
            fetchXml += "</entity>";
            fetchXml += " </fetch>";

            subgridcontact.control.SetParameter("fetchXml", fetchXml);
            //Force the subgrid to refresh
            subgridcontact.control.refresh();
       }

No comments:

Post a Comment