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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Copy quote entity by plugin

(0) ShareShare
ReportReport
Posted on by 115

I would like to clone a quote by plugin but it fails with "Cannot insert duplicate key row in object 'dbo.QuoteBase' with unique index 'ndx_Unique_Quote'. The duplicate key value is redacted".

I am trying to simply copy the entity to an new one and create that one. I am aware of the problematic with primary key field so i remove the old guid in property id and the attributes list. I am also replacing the values of Money/EntityReference/OptionSetValue-fields with a function i found in antoher thread to prevent conflicts there.

Heres my code - "OriginalQuote.record" is the orginal entity retrieved by:

Entity record = client.Retrieve(
    ((EntityReference)context.InputParameters["Target"]).LogicalName,
    ((EntityReference)context.InputParameters["Target"]).Id,
    new ColumnSet(true)
);

The function i use for replacing sepecial attributes:

public static Entity CloneEntitySandbox(Entity entityToClone)
{
    var newEntity = new Entity(entityToClone.LogicalName);

    var systemAttributes = new List();
    systemAttributes.Add("createdon");
    systemAttributes.Add("createdby");
    systemAttributes.Add("modifiedon");
    systemAttributes.Add("modifiedby");
    systemAttributes.Add("owninguser");
    systemAttributes.Add("owningbusinessunit");


    foreach (var attribute in entityToClone.Attributes
        .Where(x => x.Key != entityToClone.LogicalName   "id")
        .Where(x => !systemAttributes.Contains(x.Key)))
    {

        switch (attribute.Value.GetType().Name)
        {
            case "Money":
                var m = attribute.Value as Money;
                newEntity[attribute.Key] = new Money(m.Value);
                break;
            case "EntityReference":
                var er = attribute.Value as EntityReference;
                newEntity[attribute.Key] = new EntityReference(er.LogicalName, er.Id);
                break;
            case "OptionSetValue":
                var os = attribute.Value as OptionSetValue;
                newEntity[attribute.Key] = new OptionSetValue(os.Value);
                break;
            default:
                newEntity[attribute.Key] = attribute.Value;
                break;
        }

    }

    return newEntity;
}

The cloning logic itself:

Entity newQuote = CloneEntitySandbox(OriginalQuote.record);

//Set the EntityState to null, so that a new cloned record can be created
newQuote.EntityState = null;
//remove the PrimaryId of the record otherwise will show you error
newQuote.Attributes.Remove(newQuote.LogicalName   "id");
newQuote.Attributes.Remove("sit_vorgang");
newQuote.Id = Guid.NewGuid();

newQuote.Attributes["name"]  = " (copy)";
tracingService.Trace("newQuote.name: "   newQuote.Attributes["name"]);
tracingService.Trace("newQuote.Id: "   newQuote.Id.ToString());

client.Create(newQuote);

What could cause these problems?

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    To be honest I'm not sure why do you need all that code. The following should be enough:

    Entity record = client.Retrieve(
        ((EntityReference)context.InputParameters["Target"]).LogicalName,
        ((EntityReference)context.InputParameters["Target"]).Id,
        new ColumnSet(true)
    );
    
    record.Id = Guid.Empty;
    record.Attributes.Remove("quoteid");
    record.Attributes.Remove("sit_vorgang");
    record.Attributes.Remove("statecode");
    record.Attributes.Remove("statuscode");
    
    //I believe this is the attribute that blocks the create
    record.Attributes.Remove("quotenumber");
    
    client.Create(record);

  • AndreasB83 Profile Picture
    115 on at

    Thank you for the quick response!

    "To be honest I'm not sure why do you need all that code. The following should be enough:"

    Pure despair ; -)

    I removed the CloneEntitySandbox method and added the suggested removal off the quotenumber.

    That seems to fix the problem - the record is cloned successfully!

    So my conclusion is that the quotenumber field is an index field also. Where is such an information communicated in the docs? I looked in XRMToolbox's Entity Relation Diagramm and it is just a normal column.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Andreas,

    I believe it's just the knowledge I got during years implementing the system. I'm not sure if it's documented anywhere.

    If my reply (or replies) answers your question take a minute and verify the answer. You can do it by clicking "Yes" at the top of the reply (or replies) under the label "Does this answer your question?"

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 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans