Saturday, March 3, 2018

MS CRM ActivityParty REST Queries

I recently had to create a rollup display showing all activities under a given entity. Pulling the sender or receiver out of an email activity requires a few steps.

Adding confusion the email entity has a text field ToRecipients which is an accumulated convenience text field. I would avoid using it as it is not complete if an email is sent to an email address that is not associated with a CRM contact, user etc. In this case the ToRecipients field will be null and the activity party AddressUsed field must be evaluated.

To fully reach the sender and receivers of email activities from an ActivityPointer query, the REST query needs to expand to include the activitypointer_activity_parties link table. You will need the ParticipationTypeMask to find out the role of each related activityparty, and both the AddressUsed and PartyId attributes.

Below is a sample query for all activities (ActivityPointer) related to an entity.
function retrieveActivities() { var caseId = AllCases[ProcessCaseIndex].Id; var parameters = "$select=ActivityId, RegardingObjectId, Subject, ActivityTypeCode, CreatedOn, ScheduledStart, Description, StateCode, OwnerId"; var expandParameters = ",activitypointer_activity_parties/ParticipationTypeMask,activitypointer_activity_parties/PartyId,activitypointer_activity_parties/AddressUsed "; var expand = "&$expand=activitypointer_activity_parties"; var filter = "&$filter=RegardingObjectId/Id eq (guid'" + caseId + "')"; var orderby = "&$orderby=CreatedOn"; var options = parameters + expandParameters + expand + orderby + filter; SDK.REST.retrieveMultipleRecords("ActivityPointer", options, retrieveActivitiesCallBack, function (error) { alert(error.message); }, activitiesRetrieveComplete); }

The ActivityPointer.ActivityTypeCode specifies the activity types:
  • email, appointment, phonecall, task, letter, fax, serviceappointment, campaignactivity, bulkoperation
  • caseresolution, opportunityclose, quoteclose, campaignresponse
The ParticipationTypeMask specifies the activity party role.
1 From, 2 To, 3 CC, 4 BCC, Owner 9, Regarding 8, and a number of others.

In the following code snipet we are just looking for the Sender or From activity party for email activities. The PartyId.Name is used if available otherwise it looks at the AddressUsed field, but this field is used in all the other email activity parties (From, To, CC, BCC).

function retrieveActivitiesCallBack(retrievedActivities) { for (var i = 0; i < retrievedActivities.length; i++) { var activity = retrievedActivities[i]; var type = activity.ActivityTypeCode; if (type == "email") { var activityParties = activity.activitypointer_activity_parties.results; for (var j = 0; j < activityParties.length; j++) { var activityMask = activityParties[j].ParticipationTypeMask.Value; // Sender Only if (activityMask == 1) { if (activityParties[j].PartyId.Name == null) { senderName = activityParties[j].AddressUsed } else { senderName = activityParties[j].PartyId.Name; } break; } } }
...