I'm trying to write a plugin that will copy the description of the email into the description of a case when using the standard conver-to-functionality present in the Outlook client.
I've registered my plugin with Create on the case entity which triggers, but when I'm trying to find the email related to the case I'm getting no returns. I'm using a query expression, and here is my code:
Entity currCase = service.Retrieve("incident", entity.Id, new ColumnSet(new string[] { "incidentid", "description" }));
QueryExpression query = new QueryExpression();
// Setup the query for the email entity
query.EntityName = "email";
// Specify the columns to retrieve
ColumnSet columns = new ColumnSet(new string[] { "activityid", "description", "regardingobjectid", "subject" });
query.ColumnSet = columns;
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
ConditionExpression condition = new ConditionExpression("regardingobjectid", ConditionOperator.Equal, new Guid(currCase["incidentid"].ToString()));
query.Criteria.AddCondition(condition);
EntityCollection emails = service.RetrieveMultiple(query);
why is this code returning 0, zero, emails? When I open the case the email is there!!
My second approach to this use two events, Create of case, and update on email. But when doing almost the excact same thing I'm getting 5000 emails in return. I'm able get the correct description, but when my query expression is returning 5000 rows, I know there is something wrong...
Entity currEmail = service.Retrieve("email", entity.Id, new ColumnSet(new string[] { "activityid", "regardingobjectid", "description" }));
Guid regardingGuid = ((EntityReference)currEmail["regardingobjectid"]).Id;
QueryExpression query = new QueryExpression();
query.EntityName = "incident";
ColumnSet columns = new ColumnSet(new string[] { "incidentid", "description" });
query.ColumnSet = columns;
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
ConditionExpression condition = new ConditionExpression("incidentid", ConditionOperator.Equal, regardingGuid);
query.Criteria.AddCondition(condition);
EntityCollection incidents = service.RetrieveMultiple(query);
if (incidents.Entities.Count == 1) {
foreach (Entity inc in incidents.Entities) {
QueryExpression emailQuery = new QueryExpression();
emailQuery.EntityName = "email";
ColumnSet cols = new ColumnSet(new string[] { "activityid", "regardingobjectid" });
emailQuery.ColumnSet = cols;
emailQuery.Criteria = new FilterExpression();
emailQuery.Criteria.FilterOperator = LogicalOperator.And;
ConditionExpression cond = new ConditionExpression("regardingobjectid", ConditionOperator.Equal, new Guid(inc["incidentid"].ToString()));
query.Criteria.AddCondition(cond);
EntityCollection emails = service.RetrieveMultiple(emailQuery);
inc["description"] = HTMLToText(currEmail.Attributes["description"].ToString()) + " :: " + emails.Entities.Count.ToString() + " :: " + inc["incidentid"].ToString();
service.update(inc);
}
}