Home

Dec 1, 2011

CRM 2011 : How do specific condition for Activity Party in Query Expression?

 

I was trying to retrieve campaign response by customer using query expression. In CRM 2011, customer is of type activity party in campaign response. Activity Party records are stored in activity pointer entity which needs to link with the parent entity. Below snippets can retrieve all campaign responses by particular customer(Activity Party)

// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();
// Set the condition for the retrieval to retrieve all activities that belong to the current user.
condition.AttributeName = "partyid";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(contactId);

// Build the filter based on the condition.
FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions.Add(condition);

// Create a LinkEntity to link the activity participant to the activity.
LinkEntity link = new LinkEntity();

// Set the properties of the LinkEntity.
link.LinkCriteria = filter;

// Set the linking entity to be the activity.
link.LinkFromEntityName = "activitypointer";

// Set the attribute being linked to to be the activityid.
link.LinkFromAttributeName = "activityid";

// Set the entity being linked to to be the activityparty.
link.LinkToEntityName = "activityparty";

// Set the attribute linking to the activityparty to be the activityid.
link.LinkToAttributeName = "activityid";

QueryExpression query = new QueryExpression();
query.EntityName = "campaignresponse";
query.Criteria = new FilterExpression();
query.LinkEntities.Add(link);
query.ColumnSet = new ColumnSet(new String[] { "activityid" });

var campaignresponses = (EntityCollection)service.RetrieveMultiple(query);
Happy Coding

No comments: