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 :
Customer experience | Sales, Customer Insights,...
Answered

Create a new record in the Quote table

(0) ShareShare
ReportReport
Posted on by 95

Hello.

I am trying to create a duplicate record of the Quote table with another id, but it always gives me an error 'The provided key was not found in the dictionary'.

My code is this:

public void CopyQuote(Entity entity, IOrganizationService service)
{
   try
   {
   string newquote = "test";
   Entity regnew = new Entity("quote");

   foreach (KeyValuePair<String, Object> attribute in entity.Attributes)
   {
      if (attribute.Key == "quoteid")
      {
      Guid id = Guid.NewGuid();
      regnew ["quoteid"] = id;

      }
     else if (attribute.Key == "name")
           regnew ["name"] = newquote;
     else if (attribute.Key != "quotenumber")
          regnew ["quotenumber"] = newquote;
     else if (attribute.Key != "statecode")
        regnew ["statecode"] = 0;
     else if (attribute.Key != "statuscode")
        regnew ["statuscode"] = 1;
     else
     {
         string attributeName = attribute.Key;
        object attributeValue = attribute.Value;
        regnew [attributeName] = attributeValue;
     }
   }

   service.Create(regnew);
   }
   catch (Exception ex)
   {
   throw new InvalidPluginExecutionException(ex.Message);
   }

}

I have tried with a simple:

Entity regnew = new Entity("quote");

Guid id = Guid.NewGuid();
regnew ["quoteid"] = id;

service.Create(regnew);
   

But it gives me the same error. It has to be something that escapes me, because I have tried the same thing with a custom table and it duplicates it without problems.

May be you can't duplicate on tables that aren't custom?

Greetings and thanks.

      

   

  • Ray Profile Picture
    1,537 on at

    Can you share about the Exception Stack Trace for this exception?

    As far as I know, you do not need to generate a Guid for entity creation, platform has its own algorithm that generates GUIDs in sequential order, this has better performance.

    Besides, I am confused about this part of code:

    else if (attribute.Key != "quotenumber")
        regnew ["quotenumber"] = newquote;
    else if (attribute.Key != "statecode")
        regnew ["statecode"] = 0;
    else if (attribute.Key != "statuscode")
        regnew ["statuscode"] = 1;

    I think it should be "==" right?

  • Verified answer
    Ray Profile Picture
    1,537 on at

    You may read about this article

  • Antonio Llanero Profile Picture
    95 on at

    Hello Ray Gan. Thanks for answering.

    You were right in the first comment. It was ==. I had missed changing it in several checks.

    But the second comment really helped me. I was missing to be able to read the attributes of the original entity:

    Entity account = OrganizationService.Retrieve("account", Guid.Parse("6a7cb8d1-7038-ea11-a813-000d3a385a1c"), new ColumnSet(true));

    Greetings and thanks for everything.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Syed Aqib Raza Profile Picture

Syed Aqib Raza 102

#2
11manish Profile Picture

11manish 88 Super User 2026 Season 2

#3
ParthPatel249 Profile Picture

ParthPatel249 84

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans