Hi All,
I am getting strange error while creating an Email activity for an Opportunity record.
For this I have create one custom email template and which I have used while sending an email.
Error Message: Attribute 'partyobjecttypecode' cannot be NULL
Below is the function that I have developed, kindly help in this regard.
public void ProcessEmail(IOrganizationService service, Entity OppRecord, string TemaplteName)
{
try
{
string subject = "";
Guid templateId = new Guid();
string contactEmailId = string.Empty;
StringBuilder description = new StringBuilder();
EntityCollection EC = new EntityCollection();
string ContactId = ((EntityReference)OppRecord["ownerid"]).Id.ToString();
((Microsoft.Xrm.Sdk.EntityReference)((new System.Linq.SystemCore_EnumerableDebugView<System.Collections.Generic.KeyValuePair<string, object>>(OppRecord.Attributes)).Items[4].Value)).Id.ToString();
Entity contact = service.Retrieve("systemuser", new Guid(ContactId), new ColumnSet(true));
if (contact.Attributes.Contains("internalemailaddress"))
contactEmailId = Convert.ToString(contact.Attributes["internalemailaddress"]);
else
return;
#region Getting Template
EntityCollection templateEntityCollection = new Microsoft.Xrm.Sdk.EntityCollection();
QueryExpression queryBuildInTemplates = new QueryExpression
{
EntityName = "template",
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression()
};
queryBuildInTemplates.Criteria.AddCondition("title", ConditionOperator.Equal, TemaplteName);
EC = service.RetrieveMultiple(queryBuildInTemplates);
if (EC.Entities.Count > 0)
{
templateId = (Guid)EC.Entities[0].Attributes["templateid"];
}
InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
{
TemplateId = templateId,
ObjectId = OppRecord.Id,
ObjectType = OppRecord.LogicalName
};
InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);
if (instTemplateResp.EntityCollection.Entities.Count > 0)
{
EntityCollection EColl = instTemplateResp.EntityCollection;
subject = instTemplateResp.EntityCollection.Entities[0]["subject"].ToString();
description.Append(Convert.ToString(instTemplateResp.EntityCollection.Entities[0]["description"]));
}
#endregion
// Entity ap = new Entity();
// Entity ap1 = new Entity();
// Entity ap2 = new Entity();
//ap = new Entity("activityparty");
#region Create Email Activity
CreateRequest reqCreate = new CreateRequest();
Entity emailCreate = new Entity("email");
emailCreate["subject"] = subject;
emailCreate["description"] = description.ToString();
Entity toParty = new Entity("activityparty");
toParty.Attributes.Add("partyid", contactEmailId);
Entity fromParty = new Entity("activityparty");
fromParty.Attributes.Add("partyid", "MBSADM@maxis.com.my");
EntityCollection collFromParty = new EntityCollection();
collFromParty.EntityName = "systemuser";
collFromParty.Entities.Add(fromParty);
EntityCollection collToParty = new EntityCollection();
collToParty.EntityName = "systemuser";
collToParty.Entities.Add(toParty);
emailCreate.Attributes.Add("from", collFromParty);
emailCreate.Attributes.Add("to", collToParty);
// This is how you assign a regarding object
emailCreate["regardingobjectid"] = new EntityReference(OppRecord.LogicalName, OppRecord.Id);
reqCreate.Target = emailCreate;
CreateResponse resp = (CreateResponse)service.Execute(reqCreate);
#endregion
}
catch (Exception excp)
{ //throw new InvalidPluginExecutionException(excp.Message); }
}
}
}
