web
You’re offline. This is a read only version of the page.
close
Skip to main content
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.

      

   

I have the same question (0)
  • Ray Profile Picture
    1,537 on at
    RE: Create a new record in the Quote table

    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
    RE: Create a new record in the Quote table

    You may read about this article

  • Antonio Llanero Profile Picture
    95 on at
    RE: Create a new record in the Quote table

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 179 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 110

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 61 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans