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)

Using Javascript to display Activities in a Sub-Grid, can't get it to work

(0) ShareShare
ReportReport
Posted on by

Hi y'all,

So I am attempting to get a Subgrid on a custom entity to dynamically display all activities where any contact related to the custom entity is a party to said activities.

I can get the data I need using advanced find, here is the FetchXML:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="activitypointer" >
<attribute name="activitytypecode" />
<attribute name="subject" />
<attribute name="statecode" />
<attribute name="prioritycode" />
<attribute name="modifiedon" />
<attribute name="activityid" />
<attribute name="instancetypecode" />
<attribute name="community" />
<order attribute="modifiedon" descending="false" />
<link-entity name="activityparty" from="activityid" to="activityid" link-type="inner" alias="af" >
<link-entity name="contact" from="contactid" to="partyid" link-type="inner" alias="ag" >
<filter type="and" >
<condition attribute="customentity" operator="eq" uiname="name" uitype="customentity" value="{redacted}" />
</filter>
</link-entity>
</link-entity>
</entity>
</fetch>

But of course, this only displays the activities for the hard coded contacts.

I eventually found the following Javascript and have been testing it to no avail.  Can someone please take a quick look and tell me what I am doing wrong?

function filterSubGrid() {
    var householdsGrid = document.getElementById("hhactivities"); //grid to filter 
    if (householdsGrid == null) { //make sure the grid has loaded 
        setTimeout(function () { filterSubGrid(); }, 2000); //if the grid hasn’t loaded run this again when it has 
        return;
    }

    var householdValue = Xrm.Page.data.entity.getId(); //field to filter by 

    var householdId = "00000000-0000-0000-0000-000000000000"; //if filter field is null display nothing 
    if (householdValue != null) {
        var householdId = householdValue[0].id;
    }

    //fetch xml code which will retrieve all the activities related to all contacts in the household
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
    "  <entity name='activitypointer'>" +
    "    <attribute name='activitytypecode' />" +
    "    <attribute name='subject' />" +
    "    <attribute name='statecode' />" +
    "    <attribute name='prioritycode' />" +
    "    <attribute name='modifiedon' />" +
    "    <attribute name='activityid' />" +
    "    <attribute name='actualend' />" +
    "    <attribute name='instancetypecode' />" +
    "    <attribute name='community' />" +
    "    <order attribute='actualend' descending='false' />" +
    "    <link-entity name='activityparty' from='activityid' to='activityid' link-type='inner' alias='aa' >" +
    "      <link-entity name='contact' from='contactid' to='partyid' link-type='inner' alias='ab' >" +
    "        <filter type='and'>" +
    "          <condition attribute='customentity' operator='eq' uitype='customentity' value='" + householdId + "' />" +
    "        </filter>" +
    "      </link-entity>" +
    "    </link-entity>" +
    "  </entity>" +
    "</fetch>";

    householdsGrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid   
    householdsGrid.control.refresh(); //refresh the sub grid using the new fetch xml 
}


Thanks in advance for any insight

*This post is locked for comments

I have the same question (0)
  • Manju Augustine Profile Picture
    140 on at
  • Financial IT Guy Profile Picture
    on at

    Thanks for the link but that's not quite what I am looking for; The code I posted above used to work but no longer works and I believe it's due to changes in the way Dynamics allows access to the DOM.  I just can't figure out why it's not working :-(

  • Verified answer
    Tomas Prokop Profile Picture
    590 on at

    Hi,

    I use this code (nasty timeout hack) and it currently works in 8.1, 8.2, 9.0.1 and 9.0.2. How do you call it? I use Form OnLoad event handler.

    function FilterNotes() {
        Xrm.Page.ui.controls.get('Meeting_Notes').setVisible(false);
    
        //Handle jQuery
        if (typeof($) === 'undefined') {
            $ = parent.$;
            jQuery = parent.jQuery;
            if (typeof($) === 'undefined') {
                $ = parent.jQuery;
            }
        }
    
        var entityId = Xrm.Page.data.entity.getId();
    
        var sg = window.parent.document.getElementById("Meeting_Notes");
    
        if (sg == null) {
            //It's not loaded yet
            setTimeout(function() {
                FilterNotes();
            }, 2000);
            return;
        }
    
        if (Xrm.Page.context.client.getClient() != "Web") {
            // Add code that should not run in CRM for phones and tablets here
            Xrm.Page.ui.controls.get('Meeting_Notes').setVisible(false);
        } else {
            Xrm.Page.ui.controls.get('Meeting_Notes').setVisible(true);
    
    
            //fetch xml code 
            var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                "<entity name='tntg_meetingnotes'>" +
                "<attribute name='tntg_meetingnotesid' />" +
                "<attribute name='tntg_name' />" +
                "<attribute name='createdon' />" +
                "<order attribute='tntg_name' descending='true' />" +
                "<link-entity name='tntg_tntg_meetingnotes_contact' from='tntg_meetingnotesid' to='tntg_meetingnotesid' visible='false' intersect='true'>" +
                "<link-entity name='contact' from='contactid' to='contactid' alias='aq'>" +
                "<filter type='and'>" +
                "<condition attribute='parentcustomerid' operator='eq' value='" + entityId + "' />" +
                "</filter>" +
                "</link-entity>" +
                "</link-entity>" +
                "</entity>" +
                "</fetch>";
    
            if (sg.control != null) {
                sg.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid   
                sg.control.refresh(); //refresh the sub grid using the new fetch xml
            } else {
                setTimeout(FilterNotes, 500);
    
            }
    
        }
    }


  • Financial IT Guy Profile Picture
    on at

    That did the trick!  Thanks Thomas!  Yes, I call it using Form OnLoad.  I like the timeout hack, mine kept throwing an error at "Return", but I now believe that it was related to the "document.get" needing to be updated to "Xrm.Page.ui"

    Again, many thanks!

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