Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Getting null reference exception at Guid emailId = service.Create(email);

Posted on by

Hi,

I want to send an email from a custom workflow. for that, I have written code.

email.From = new[] { fromParty };
email.To = new[] { toParty };

tracingService.Trace("To and From are added ");
email.RegardingObjectId = new EntityReference(Incident.EntityLogicalName, caseEntity.Id); 
Guid emailId = service.Create(email);
SendEmailRequest sendRequest = new SendEmailRequest();
sendRequest.EmailId = emailId;
sendRequest.TrackingToken = string.Empty;
sendRequest.IssueSend = true;
service.Execute(sendRequest);

But when I changed email.RegardingObjectId code I got nullreference exception.

email.RegardingObjectId = new EntityReference(ActivityParty.EntityLogicalName, emailEntity.Id); //This line //throws exception.

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting null reference exception at Guid emailId = service.Create(email);

    new EntityReference(Incident.EntityLogicalName, IncidentGuidId);

    This works forme

  • Ashutosh9 Profile Picture
    Ashutosh9 on at
    RE: Getting null reference exception at Guid emailId = service.Create(email);

    I tried with this

    new EntityReference(Incident.EntityLogicalName, emailEntity.Id);

    But still not getting anything

  • Ashutosh9 Profile Picture
    Ashutosh9 on at
    RE: Getting null reference exception at Guid emailId = service.Create(email);

    I have the portal comment activity. From that I am building my emailEntity object.

    Below is the code for that

    Entity target = (Entity)context.InputParameters["Target"];

    Guid activityId = new Guid(target.Attributes["activityid"].ToString());

    Entity portalCommentEmailEntity = service.Retrieve(target.LogicalName, activityId, new

    ColumnSet(true));

    var attriutes = portalCommentEmailEntity.Attributes;

    EntityCollection ToEmail = portalCommentEmailEntity.GetAttributeValue<EntityCollection>("to");

    if (ToEmail != null) { tracingService.Trace("Number of Records inside To: " + ToEmail.Entities.Count.ToString()); }

    var email = ToEmail.Entities.FirstOrDefault<Entity>();

    EntityReference emailEntity = (EntityReference)email.Attributes["partyid"];

  • Suggested answer
    ARIFNIIT Profile Picture
    ARIFNIIT 1,391 on at
    RE: Getting null reference exception at Guid emailId = service.Create(email);

    Are you using early bind entity classes? Can you share complete code? May be you are getting exception because of you setting ActivityParty in regarding field. you can set any entity in regarding for which Activity will be enable.

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Getting null reference exception at Guid emailId = service.Create(email);

    Where are you defining emailEntity ? I can't see it anywhere else in the code, so if it's null, then you'd get this error.

    Also, I don't think you can set the RegardingObjectId to a record of type ActivityParty. CRM will create an ActivityParty record based on the RegardingObjectId, but the RegardingObjectId should be set to different type of entity (e.g. Incident)

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans