Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Error while creating an Email Activity

Posted on by Microsoft Employee

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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Error while creating an Email Activity

    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans