web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Error while creating an Email Activity

(0) ShareShare
ReportReport
Posted on by

Hi All,

I am trying to create an email message and also to list that email under Activities on Case form. But I get the error that "Regardingobjecttypecode cannot be null when regardingobjectid is not null" .  Can anyone tell me how to set regardingobjecttypecode and how to list the email activity under Activities. Here's the code :

Entity emailTemplateInstance = ((EntityCollection)instTemplateResp.Results["EntityCollection"])[0];
emailTemplateInstance.Attributes["subject"] = ((EntityCollection)instTemplateResp.Results["EntityCollection"])[0].GetAttributeValue<string>("subject");
emailTemplateInstance.Attributes["description"] = emailTemplateInstance.Attributes["description"].ToString().Replace("%7bCaseNumber%7d", caseNumber);

Entity email = new Entity("email");

Entity senderParty = new Entity("activityparty");
Entity receiverParty = new Entity("activityparty");
receiverParty["partyid"] = new EntityReference("contact", emailReceiversId);
senderParty["partyid"] = new EntityReference("systemuser", emailSendersId);

Entity[] emailReceivers = { receiverParty };
Entity[] emailSenders = { senderParty };

email["to"] = emailReceivers;
email["from"] = emailSenders;
email["subject"] = emailTemplateInstance.Attributes["subject"];
email["description"] = emailTemplateInstance.Attributes["description"];
email["regardingobjectid"] = new EntityReference("case", caseId);

Guid emailId = service.Create(email);

SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = emailId,
TrackingToken = "",
IssueSend = true
};

SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hi Bob,

    I noticed this line of code where you have "case", which i think you might want to specify the"incident" entity:

    email["regardingobjectid"] = new EntityReference("case", caseId);

    Change to 

    email["regardingobjectid"] = new EntityReference("incident", caseId);

    Let us know if it worked.

    Cheers!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans