Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

ID of an entity does not exist

(0) ShareShare
ReportReport
Posted on by 390

Hi, this is a continuation of another thread. I am trying to send an email once a custom entity is created. The plugin is registered on Create and  Post. my code is below: 

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity travelDetail = (Entity)context.InputParameters["Target"];

                if (travelDetail.LogicalName != "gg_traveldetail") { return; }

                try
                {

                    EntityReference sConRef = travelDetail.GetAttributeValue<EntityReference>("gg_consultantstaffingrole");
                    EntityReference sNurRef = travelDetail.GetAttributeValue<EntityReference>("gg_nursestaffingrole");

                    if (travelDetail.Attributes.Contains("gg_bookingcomplete"))
                    {
                        if (travelDetail.GetAttributeValue<bool>("gg_bookingcomplete"))
                        {

                            Guid userId = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;

                            Guid travelregardingobjectid = Guid.Empty; // new Guid(context.OutputParameters["id"].ToString());
                            string regardingobject = string.Empty;//"gg_traveldetail";
                            string emailSubject = "A travel detail has just been completed";
                            string emailBody = "Plesae refer to the completed travel that has just been completed by either a consultant or a nurse";

                            if (context.OutputParameters.Contains("id"))
                            {
                                travelregardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                                regardingobject = "gg_traveldetail";
                            }

                            if (sConRef != null)
                            {

                                //Entity image = (Entity)context.PreEntityImages["ImageDist"];

                                Entity conRef = service.Retrieve(sConRef.LogicalName, sConRef.Id, new ColumnSet(true));

                                EntityReference con = conRef.GetAttributeValue<EntityReference>("new_consultant");

                                Entity consultant = service.Retrieve(con.LogicalName, con.Id, new ColumnSet(new string[] { "new_name", "emailaddress" }));


                                //Entity email = new Entity("email");
                                SendEmail(service, consultant, consultant.Id, userId, travelregardingobjectid, emailBody, emailSubject, regardingobject);
                            }

The plugin throws the error when SendEmail() (the last line) method is called. Error: "gg_traveldetail with id xxxxxxxxx Does not exist". The id in this case is gotten from the context.OutputParemeter. The email method code is below

private void SendEmail
            (IOrganizationService service,Entity receiever, Guid recieverUserId, Guid senderUserId, Guid regardingObjectId, string emailBody, string emailSubject, string entity)

        {

            Entity email = new Entity();
            email.LogicalName = "email";

            //Set regarding object property (i.e. The entity record, which u want this email associated with)

            EntityReference regardingObject = new EntityReference(entity, regardingObjectId);
            email.Attributes.Add("regardingobjectid", regardingObject);

            Entity fromParty = new Entity("activityparty");
            fromParty["addressused"] = "victor@co.uk";
            EntityReference from = new EntityReference("systemuser", senderUserId);
            fromParty.Attributes.Add("partyid", from);

            //Derive to party

            Entity toParty = new Entity("activityparty");
            toParty["addressused"] = "victor@co.uk";
            EntityReference to = new EntityReference(receiever.LogicalName, recieverUserId);
            toParty.Attributes.Add("partyid", to);

            EntityCollection collFromParty = new EntityCollection();
            collFromParty.EntityName = "systemuser";
            collFromParty.Entities.Add(fromParty);

            EntityCollection collToParty = new EntityCollection();
            collToParty.EntityName = "systemuser";
            collToParty.Entities.Add(toParty);

            email.Attributes.Add("from", collFromParty);
            email.Attributes.Add("to", collToParty);

            //Set subject & body properties
            email.Attributes.Add("subject", emailSubject);
            email.Attributes.Add("description", emailBody);

            //Create email activity

            Guid emailID = service.Create(email);

            //Sending email

            SendEmailRequest reqSendEmail = new SendEmailRequest();
            reqSendEmail.EmailId = emailID;//ID of created mail
            //reqSendEmail.TrackingToken = "";
            reqSendEmail.IssueSend = true;

            SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);

        }
    }

   
}

Thanks Guys for helping.

*This post is locked for comments

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 390 on at
    RE: ID of an entity does not exist

    Hi Arun Vinoth, thanks alot, I did all the test you recommended and the Async option finally worked. Registered the plugin on asynchronously and I confirmed it worked on the Job System.    Arun Vinoth Temmy Wahyu Raharjo Thanks guys for the time.

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: ID of an entity does not exist

    One more suggestion, while debugging press F11 and enter into SendEmail method to see which line is throwing error.

    Also comment out the below line & see which one is failing - service.Create(email) or SendEmailRequest?

    SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: ID of an entity does not exist

    Hi Victor, maybe instead of using OutputParameters, try to just using travelDetail.Id. Also because this one is regardingobject, make sure that your entity is activity entity. Then the other is instead of calling WhoAmIRequest, you can get the userid from PluginExecutionContext.UserId.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: ID of an entity does not exist

    Hi Vicotr,

    I looked into the code and the only place you are referring to travel details entity is while setting it as regarding on email. If this is the complete code and you have registered you plugin on port create then you should not get the error "gg_traveldetail with id xxxxxxxxx Does not exist".

    You may get some other errors but not this one. Can you share  screenshot of this error

    Hope this helps.

  • Verified answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: ID does not exist

    Can you verify if this is synchronous or asynchronous? Switch & test. Is this working when it’s not debugged?

    Also comment out the below line & try to set a different regarding.

    EntityReference regardingObject = new EntityReference(entity, regardingObjectId);

    May be for example: this will set sender as regarding. Let’s see if this works.

    EntityReference regardingObject = new EntityReference("systemuser", senderUserId);

    My guess is the DB transaction of creating travel detail record is not yet committed, hence creation of email record failed with this error, where you are referring created record as regarding.

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: ID of an entity does not exist

    Can you verify if this is synchronous or asynchronous? Switch & test. Is this working when it’s not debugged?

    Also comment out the below line & try to set a different regarding.

    EntityReference regardingObject = new EntityReference(entity, regardingObjectId);

    May be for example: this will set sender as regarding. Let’s see if this works.

    EntityReference regardingObject = new EntityReference("systemuser", senderUserId);

    My guess is the DB transaction of creating travel detail record is not yet committed, hence creation of email record failed with this error, where you are referring created record as regarding.

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 390 on at
    RE: ID does not exist

    Hi guys, this thread is going too long, i have opened another thread on this here : [View:https://community.dynamics.com/crm/f/117/t/290678:750:50] Arun Vinoth 

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 390 on at
    RE: ID does not exist

       And i tried created a task before sending the email, but the task was not created, so i guess thats why the problem comes from.

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 390 on at
    RE: ID does not exist

    Hi , I am still getting the error of "gg_traveldetail id xxxxxxxxxx does not exist", really do not know what to do, so still looking for solution.

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: ID does not exist

    So whats the current state? Are you still getting the error? Or you are still looking for solution? As you have identified, the id of the record is generated when you actualy create the record and in port create operation of plugin.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,569 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans