Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Create a new record in the Quote table

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.

      

   

  • Antonio Llanero Profile Picture
    Antonio Llanero 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.

  • Verified answer
    Ray Profile Picture
    Ray 1,505 on at
    RE: Create a new record in the Quote table

    You may read about this article

  • Ray Profile Picture
    Ray 1,505 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?

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans