Skip to main content

Notifications

Announcements

No record found.

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

Copy quote entity by plugin

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?

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Copy quote entity by plugin

    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?"

  • AndreasB83 Profile Picture
    AndreasB83 115 on at
    RE: Copy quote entity by plugin

    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.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Copy quote entity by plugin

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

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans