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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create quotedetails c# givs error unexpected error

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Sreevalli Profile Picture
    3,264 on at

    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

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    Hi There,

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

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    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

  • Community Member Profile Picture
    on at

    5633.temp.PNG

    As you can se there are no inner exception.

    /Niklas

  • Community Member Profile Picture
    on at

    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);
                }            
            }]
    


  • Verified answer
    Kokulan Profile Picture
    18,054 on at

    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
    on at

    Thanks!

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 Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans