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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

ID of an entity does not exist

(0) ShareShare
ReportReport
Posted on by 410

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

I have the same question (0)
  • Victor Onyebuchi Profile Picture
    410 on at

    Hi Guys, I am trying to send an email when a record of a entity is created, but i keep getting the error after debugging - "custom enity with Id = xxxxxxxxxxx Does not exist". My code is below:

    f (travelDetail.Attributes.Contains("gg_bookingcomplete"))
                        {
                            if (travelDetail.GetAttributeValue<bool>("gg_bookingcomplete"))
                            {
    
                                Guid userId = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;
    
                                Guid travelregardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                                string regardingobject = "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 (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, travelDetail, consultant, consultant.Id, userId, travelregardingobjectid, emailBody, emailSubject, regardingobject);
                                }

    and it fails at the SendEmail() method. The code for the email is below:

    private void SendEmail
                (IOrganizationService service,Entity Sender, 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@geek-guru.co.uk";
                EntityReference from = new EntityReference(Sender.LogicalName, senderUserId);
                fromParty.Attributes.Add("partyid", from);
    
                //Derive to party
    
                Entity toParty = new Entity("activityparty");
                toParty["addressused"] = "victor@geek-guru.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);
    
            }
        }

    Thank you

  • Victor Onyebuchi Profile Picture
    410 on at

    The error to be precise is "new_traveldetail with Id = xxxxxxxxxxx Does not exist"

  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    The error is super clear. The record in question is not yet created or atleast transaction is not yet complete or wrong guid mapped with entity.

    Is this post-create plugin on travel detail entity? Synchronous? Did you verify the travel detail record got created in CRM? Just open any existing travel detail record, pop-out the form & change the guid in url to check.

    I see gg_ prefix in original code but new_ prefix in above error message. Please verify that too.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Victor,

    Based on the code you have shared, the error "new_traveldetail with Id = xxxxxxxxxxx Does not exist" can occur on the statement "EntityReference from = new EntityReference(Sender.LogicalName, senderUserId);".  This error simple means that there is no "new_traveldetail" record exists with the id in the error message.  Now you are getting this id from plugin output paramter which will only work if you register it on create message, post operation.

    Check your plugin registration and if it is not create, post operation change it to this and see if it works.

    Hope this helps.

  • Victor Onyebuchi Profile Picture
    410 on at

    I can confirm that, it was registered on Create and Post-operation, that's why I am confused.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Okay and the entity in the error message is travel details? I would suggest try either debug the plugin or  break down the code and try in smaller bits i.e. instead of calling sendemail just try to create a task and see if the task is getting created. This will confirm that the id is coming from the output parameters correctly.

    var newTask = new Entity("task");

                   newTask["subject"] = "Test";

                   newTask["description"] = "test task";

                   newTask["regardingobjectid"] = new EntityReference(regardingobject, new Guid(context.OutputParameters["id"].ToString());

                   service.Create(newTask);

    Debug Plugin : dynamics365blocks.wordpress.com/.../how-to-debug-a-plugin-in-dynamics-365-online-using-plugin-profiler

    Hope this helps.

  • Victor Onyebuchi Profile Picture
    410 on at

    And the senderid used here is from the whoAmIResponse not the outputparameters.

  • Victor Onyebuchi Profile Picture
    410 on at

    Ok. I have just seen your second response,  lemme break down the code and come back to you

  • Victor Onyebuchi Profile Picture
    410 on at

    Hi Ravi, Thanks for trying to help me again. I was able to see that i was using the wrong argument in the SendEmail() method, everything works fine, but it gives me this error -

     "System.Security.SecurityException: The data contract type 'Microsoft.Xrm.Sdk.OrganizationServiceFault' cannot be serialized in partial trust because the property 'ExceptionRetriable' does not have a public getter. ---> System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
    
    at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)".

    I have googled this erro and see that only 4 result appeared and non has been answered. Do you have an idea of what this might be?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Victor,

    Could you please share some more details on when exactly you are getting this error message? Is this the complete error message? Do you have any other plugin registered on the same message? If possible please share your complete plugin code. This kind of error messages general occurs if you are doing any operation such as performing file reading writing or something similar (basically accessing local resources within the plugin) . For CRM online, you need to register the assembly in sandbox mode which has limited access so if you have any code which access local resources that it won't work.

    It is possible that this error is coming from a different plugin and not this one as in this you are just sending an email.

    Hope this helps.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
JS-09031509-0 Profile Picture

JS-09031509-0 3

#3
Ciprian  P Profile Picture

Ciprian P 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans