Filtering Sub Grid Dynamically


JULY 25, 2017 ~ DYNAMISITY

 

In Dynamics 365 filtering of sub grid is required frequently.

This example is of filtering grid based on certain criteria where data is being fetched from custom entity.

The parent entity form is having sub grid of the child entity. It is also having a lookup on which change event exist for filtering the sub-grid below:

Following Steps were utilized to implement this change:

  1. Create a lookup for filtering:
  2. Create sub grid:
  3. Create web resource with java script with:

   function
FilterSubGrid()

{
//get value of
specialityOfDoctorLookUp to be filter

var specialityOfDoctorLookUp =
Xrm.Page.getAttribute(“new_choosespecialility”).getValue();
         if
(specialityOfDoctorLookUp != null)
         {
             var
name = specialityOfDoctorLookUp[0].name;
             var
id = specialityOfDoctorLookUp[0].id;
             var
entityType = specialityOfDoctorLookUp[0].entityType;
         }
         // refer the
subgrid

         //Prescriptions is my
unique name of subgrid

         var grid =
window.parent.document.getElementById(“Prescriptions”);
         if (grid ==
null)
        {
             setTimeout(function
()
             {
                 FilterSubGrid();
             },
2000);
             return;
         }
         else
         {
             //
fetch xml code to retrieve filter data

             var
fetchXml = “<fetch version=’1.0′ output-format=’xml-platform’
mapping=’logical’ distinct=’false’>”
 +
                 ”  <entity
name=’new_prescription’>”
 +
                 ”    <attribute
name=’new_prescribedon’ />”
 +
                 ”    <attribute
name=’new_patientname’ />”
 +
                 ”    <attribute
name=’new_suggestion’ />”
 +
                 ”    <attribute
name=’new_weightofpatient’ />”
 +
                 ”    <attribute
name=’new_patientname’ />”
 +
                 ”    <attribute
name=’new_prescribedbydoctor’ />”
 +
                 ”    <attribute
name=’new_issueof’ />”
 +
                 ”    <order
attribute=’new_name’ descending=’false’ />”
 +
                 ”    <filter
type=’and’>”
 +
                 ”      <condition
attribute=’new_issueof’ operator=’eq’ uiname='”
 + name + “‘
uitype='”
 + entityType + “‘ value='” + id + “‘
/>”
 +
                 ”    </filter>”
+
                 ”  </entity>”
+
                 “</fetch>”;
             if
(grid.control != null)
             {
                 grid.control.SetParameter(“fetchXml”,
fetchXml);
                 grid.control.refresh();
             }
             else
             {
                 setTimeout(FilterSubGrid,
500);
             }
         }
     }

  1. Save and publish it.
  2. Test it

Below is the output for the following filtering functionality:

Without Filter:

With Filter:

Note: Please note this is unsupported script. Currently it works for Dynamics 365 December update.

Hope it helps You!

Please get in touch with us at deepesh@marvels.company for any of your Dynamics 365 needs.