I am trying to create a subgrid on a Form for the Contact Entity in Dynamics CRM 2015 which returns all Email, Task, Appointment and Phone Call Activities where either the Activity is Regarding the Contact for which the Form has been loaded, or where that Contact is a participant in the Activity (i.e. in the Sender or To/CC/BCC fields for an email, or on the attendee list for an Appointment).
I have added a new subgrid (called "NewActivities" for now) to my Contact Form which uses a specific Activity View which I have created (and is designed with criteria that will "never" return any results - DateCreated >= 01/01/2050) and then created a javascript function which I have included as a Web Resource in my Solution and am calling in the OnLoad event of the Form:
function DisplaySubGrid() {
var subgrid = document.getElementById("NewActivities");
if (subgrid == null) {
setTimeout('DisplaySubGrid()', 1000);
return;
}
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='regardingobjectid' />"
+ "<attribute name='ownerid' />"
+ "<attribute name='scheduledend' />"
+ "<attribute name='createdby' />"
+ "<attribute name='createdon' />"
+ "<order attribute='scheduledend' descending='false' />"
+ "<order attribute='subject' descending='false' />"
+ "<filter type='and'>"
+ "<condition attribute='activitytypecode' operator='in'>"
+ "<value>4201</value>"
+ "<value>4202</value>"
+ "<value>4210</value>"
+ "<value>4212</value>"
+ "</condition>"
+ "</filter>"
+ "<link-entity name='activityparty' from='activityid' to='activityid' alias='ae'>"
+ "<filter type='and'>"
+ "<condition attribute='participationtypemask' operator='in'>"
+ "<value>4</value>"
+ "<value>3</value>"
+ "<value>11</value>"
+ "<value>6</value>"
+ "<value>7</value>"
+ "<value>9</value>"
+ "<value>8</value>"
+ "<value>5</value>"
+ "<value>10</value>"
+ "<value>1</value>"
+ "<value>2</value>"
+ "</condition>"
+ "<condition attribute='partyid' operator='eq' uiname='" + Xrm.Page.getAttribute("fullname").getValue() + "' uitype='contact' value='" + Xrm.Page.data.entity.getId() + "' />"
+ "</filter>"
+ "</link-entity>"
+ "</entity>"
+ "</fetch>"
subgrid.control.SetParameter("fetchXml", fetchXml);
subgrid.control.refresh();
}
Hopefully the above makes sense, I'm returning attributes which match those of the Activity View which is being used in the subgrid I've set up and then filtering for the Activity Types I want and just where the Activity Party is the Contact on the page, for all participation types (this may be unnecessary I suppose, but my FetchXML was built from an Advanced Find query, so it explicitly included the values because I selected all of them).
This seems to work fine in that I see the correct list of Activities in my subgrid when the page loads, but if I click on the Subject value of any of the Activities in the list, I am taken to the "New" Form for that Activity instead of linking to the Activity that was listed. So for example, if there is an Email in my subgrid list returned, when I click on the value in the Subject column for that Activity in the subgrid, it loads the New Email form instead of taking me to that specific Email Activity record as I would expect.
Can anyone advise why this is happening and how I can resolve it?
(I do also have an additional problem, whereby sometimes when navigating to this Contact Form, the subgrid does not always refresh - even though my javascript is definitely running - and so the subgrid shows no Activity records. If I refresh the subgrid manually after page load, the results are shown - I don't understand why this is happening either. It seems to be when navigating away from the Contact Form and then using the Back in my browser to return, but I have also had it happen on a page refresh. Sorry if I'm not meant to include two questions in the same post, I can obviously post this question separately if I need to, but thought it worth mentioning as it relates to the exact same functionality I'm trying to implement.)
without trying this myself.. could be just missing the point completely.. however, there is no 'Id' attribute in your fetch.. if it were on the plugin side, there would be no record id in the retrieved record.. try adding activitypointerid (or is it activityid for that one?)