Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Create quotedetails c# givs error unexpected error

Posted on by Microsoft Employee

I´m trying to copy a quote and all quote details related. I can create the quote but the quote detail gets an error (An unexpected error occurred.)

  var quote = new Entity(_entityQuote.LogicalName);

            quote.Id = _orgSvc.Create(quote);
            quote.Attributes = _entityQuote.Attributes;
            quote.Attributes.Remove("quoteid");
            _orgSvc.Update(quote);
            
            foreach (Entity entity in _entityQuoteDetails.Entities)
            {
                entity.Id = new Guid();
                entity["quoteid"] = quote.Id;
                _orgSvc.Create(entity);
            }

60143.temp.PNG

Thanks!

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Create quotedetails c# givs error unexpected error

    Thanks!

  • Verified answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Create quotedetails c# givs error unexpected error

    Your code is failing because you are setting a GUID for an Entity Reference type.

    Your actual exception is : System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'Microsoft.Xrm.Sdk.EntityReference'

    But visual studio debugger is not able to display that message in the stack.

    Change this line  : entity["quoteid"] = quoteNew.Id;

    To this : entity["quoteid"] = quoteNew.ToEntityReference();

    It should fix the issue.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Create quotedetails c# givs error unexpected error

    Sreevalli 

    I did add the part where I remove quotedetailid but still same result.

     public void CopyQuoteHeaderQuoteProduct(EntityReference quoteRef)
            {
                var quoteOrg = _orgSvc.Retrieve(quoteRef.LogicalName, quoteRef.Id, new ColumnSet(true));
    
                Entity quoteNew = new Entity("quote");
                quoteNew.Id = _orgSvc.Create(quoteNew);            
                quoteOrg.Attributes.Remove("quoteid");
                quoteNew.Attributes = quoteOrg.Attributes;
                _orgSvc.Update(quoteNew);
    
                var quoteDetails = GetQuoteDetailsFromQuote(quoteOrg);
                
                foreach (Entity entity in quoteDetails.Entities)
                {
                    entity.Id = new Guid();
                    entity.Attributes.Remove("quotedetailid");
                    entity["quoteid"] = quoteNew.Id;
                    _orgSvc.Create(entity);
                }            
            }]
    


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Create quotedetails c# givs error unexpected error

    5633.temp.PNG

    As you can se there are no inner exception.

    /Niklas

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Create quotedetails c# givs error unexpected error

    Please share the actual inner exception that will have the detail about why it fails. I know you are setting entity.Id when you create Quote Product but since the object already has  value for quotedetailid, you might be getting cannot insert duplicate key error.  Please try removing before you execute Create quotedetail.

    Hope this helps

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: Create quotedetails c# givs error unexpected error

    Hi There,

    you should probably use cloning method described here : www.aegissofttech.com/.../entity-record-clonation-with-dynamics.html

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Create quotedetails c# givs error unexpected error

    Hi Niklas,

    If I understand correctly you are trying to clone quote and quote details. but here I think you need to remove the Id from quote details but not quote - entity.Attributes.Remove("quotedetailid"); and I am not sure if you still able to have quote.Id after removing quoteid from quote attributes collection -  quote.Attributes.Remove("quoteid");

    Try with above option but It would be easy to point out the issue if you can share the full code snip

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