web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Filter View using Javascript

(0) ShareShare
ReportReport
Posted on by

Hi everyone!

I want to filter a View that i add to Service Activity form based on the Scheduled Start date.

I use the following code:

function filterView() {
 
        var gridHtml = document.getElementById('clinicCalendarSubGrid');
		alert(gridHtml);
		var startDate = Xrm.Page.getAttribute("scheduledstart").getValue();
        if (!gridHtml)
            return;
 
     
 
        var fetchXml = [
            '<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">',
            '<entity name="ral_site">',
            '<attribute name="ral_date"/>',
            '<attribute name="ral_name"/>',
            '<attribute name="ral_resources"/>',
            '<order descending="false" attribute= "ral_date"/>',
            '<filter type="and">',
            '<condition attribute="ral_date" operator= "eq" value= '" + startDate + "'/>',
            '</filter>',
            '</entity>',
            '</fetch>'
        ].join("\n");
 
        var gridHtml = document.getElementById('clinicCalendarSubGrid');
        gridHtml.control.SetParameter("fetchXml", fetchXml);
 
        gridHtml.control.refresh();
}


I get an error instantly but the error is not informative, all i get is undefined. Anyone know what is wrong with my code and how to fix it?

Thanks a lot!

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Use this code:

    function filterView() {
    
           var gridHtml = document.getElementById('clinicCalendarSubGrid');
    
    alert(gridHtml);
    
    var startDate = Xrm.Page.getAttribute("scheduledstart").getValue();
    
           if (!gridHtml)
    
               return;
    
    var date = "";
    
           var month = "";
    
           if ((startDate.getDate()) < 10) {
    
               date = "0" + startDate.getDate();
    
               }
    
           else {
    
               date = startDate.getDate();
    
               }
    
           if ((startDate.getMonth() + 1) < 10) {
    
               month = "0" + (startDate.getMonth() + 1);
    
               }
    
           else {
    
               month = (startDate.getMonth() + 1);
    
           }
    
           var formattedStartDate = startDate.getFullYear() + "-" + month + "-" + date;
    
           var fetchXml = [
    
               '<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">',
    
               '<entity name="ral_site">',
    
               '<attribute name="ral_date"/>',
    
               '<attribute name="ral_name"/>',
    
               '<attribute name="ral_resources"/>',
    
               '<order descending="false" attribute= "ral_date"/>',
    
               '<filter type="and">',
    
               '<condition attribute="ral_date" operator= "eq" value= '" + formattedStartDate + "'/>',
    
               '</filter>',
    
               '</entity>',
    
               '</fetch>'
    
           ].join("\n");
    
           var gridHtml = document.getElementById('clinicCalendarSubGrid');
    
           gridHtml.control.SetParameter("fetchXml", fetchXml);
    
           gridHtml.control.refresh();
    
    }
    
    }


  • Community Member Profile Picture
    on at
    Refer below working code:

    function FundedSupportsSubGrid(){ var grid = parent.document.getElementById('Funded_Supports'); var clientContact = common.getValue('rsa_client_contact'); if (grid == null || grid.control == null) { setTimeout('FundedSupportsSubGrid();', 500); } else { var fetchXml = []; if (clientContact) { var today = common.getSystemDate(); var date = ""; var month = ""; if ((today.getDate()) < 10) { date = "0" + new Date().getDate(); } else { date = today.getDate(); } if ((today.getMonth() + 1) < 10) { month = "0" + (today.getMonth() + 1); } else { month = (today.getMonth() + 1); } var planExpiryDate = today.getFullYear() + "-" + month + "-" + date; fetchXml.push('<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'); fetchXml.push('<entity name="rsa_ndis_plan_support">'); fetchXml.push('<attribute name="rsa_unit_of_measure" />'); fetchXml.push('<attribute name="rsa_refund_amount_per_unit" />'); fetchXml.push('<attribute name="rsa_number_of_units_provided" />'); fetchXml.push('<attribute name="rsa_effective_date_to" />'); fetchXml.push('<attribute name="createdon" />'); fetchXml.push('<attribute name="rsa_supports_from_funding_item_description" />'); fetchXml.push('<attribute name="rsa_ndis_plan_supportid" />'); fetchXml.push('<order attribute="createdon" descending="true" />'); fetchXml.push('<filter type="and">'); fetchXml.push('<condition attribute="rsa_client_contact" operator="eq" value="' + clientContact.id + '" />'); fetchXml.push('<condition attribute="rsa_effective_date_to" operator="on-or-after" value="' + planExpiryDate + '" />'); fetchXml.push('</filter>'); fetchXml.push('</entity>'); fetchXml.push('</fetch>'); } else { fetchXml.push('<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'); fetchXml.push('<entity name="rsa_ndis_plan_support">'); fetchXml.push('<attribute name="rsa_unit_of_measure" />'); fetchXml.push('<attribute name="rsa_refund_amount_per_unit" />'); fetchXml.push('<attribute name="rsa_number_of_units_provided" />'); fetchXml.push('<attribute name="rsa_effective_date_to" />'); fetchXml.push('<attribute name="createdon" />'); fetchXml.push('<attribute name="rsa_supports_from_funding_item_description" />'); fetchXml.push('<attribute name="rsa_ndis_plan_supportid" />'); fetchXml.push('<order attribute="createdon" descending="true" />'); fetchXml.push('<filter type="and">'); fetchXml.push('<condition attribute="createdon" operator = "yesterday" />'); fetchXml.push('<condition attribute="createdon" operator="today" />'); fetchXml.push('</filter>'); fetchXml.push('</entity>'); fetchXml.push('</fetch>'); } //Setting the fetch xml to the sub grid. grid.control.SetParameter("fetchXml", fetchXml.join('')); //This statement will refresh the sub grid after making all modifications. grid.control.refresh(); } }



  • Community Member Profile Picture
    on at

    It didn't work for me unfortunately. I still get an error on window field. I don't know if above code is supported with crm online 2016 but i get an error instantly even before the Alert command.

  • Community Member Profile Picture
    on at

    that may be due to subgrid. Subgrids are often loaded after form load. Use a timeout as per my code. It works perfectly fine.

  • Community Member Profile Picture
    on at

    I did use the timeout. The strange thing is that when you add a view of an entity to another entity that view is empty on load. You have to click Save to show you the items. But even then i get an error undefined

  • Community Member Profile Picture
    on at

    Managed to get it work after a lot of trial and error. I still have to figure out how to show the records on load because the Subgrid is empty on load. Only when clicking Save the records appear

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    6,804 on at

    Hi shak,

    Since the SubGrids load asynchronously, you can't get data from OnLoad event.  But you can set an event handler for the OnLoad event of the SubGrid.

    Below article may help.

    https://msdn.microsoft.com/en-us/library/dn932137.aspx

    Cheers,

    Nadeeja

    If the answer solves your problem, please mark as Verified. Thanks.

    My Blog: http://dyn365apps.com/ - Follow me on Twitter: https://twitter.com/dyn365apps

    LinkedIn: https://www.linkedin.com/in/nadeeja

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Shak,

    Please refer the following thread.

    community.dynamics.com/.../198345

  • Community Member Profile Picture
    on at

    Can't the fetchxml bring the data from other entity into the subgrid when the form loads? It is only possible when Saving the form?

  • Verified answer
    Community Member Profile Picture
    on at

    It seems that there is no way to do that one load. Filtering on the form can be done after the form is saved

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans