Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

I am creating a clone of records but getting an error.

(0) ShareShare
ReportReport
Posted on by 814

I am creating a clone of records but getting an error.

Cannot insert duplicate key, although i am using these 2 lines. But still getting error.  --> cannot insert duplicate key

entity.Attributes.Remove(entity.LogicalName + "id");
entity.Attributes.Remove("msdyn_name");

service.Create(entity);

Once I use, this additional command no error is coming and all is working good.

entity.Id = Guid.NewGuid();

I just don't understand why is that, also I have read somewhere that creating own guid via code is not good in D365.  If i continue with this will it create aproblem in future in my search or any activity in Dynamics.

  • Ravi Fulani Profile Picture
    Ravi Fulani 814 on at
    RE: I am creating a clone of records but getting an error.

    I resolved the problem by

    entity.Id = Guid.Empty;

    now i am not getting any error, i was already doing entity.Attributes.Remove("msdyn_workorderid");

    but with this entity.Id = Guid.Empty; problem got resolved

    But I am stuck with problem as with copy of record which is work order, associated quotes are getting created which i want to stop. Any Ideas??

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,206 User Group Leader on at
    RE: I am creating a clone of records but getting an error.

    Hi, 

    Please refer to my sample code. I use it in Production for a long time. 

    IOrganizationService service = crmWorkflowContext.OrganizationService;
    ITracingService tracing = crmWorkflowContext.TracingService;
    
    EntityReference leadToCloneRef = executionContext.GetValue(this.Lead);
    Entity leadToClone = service.Retrieve(leadToCloneRef.LogicalName, leadToCloneRef.Id, new ColumnSet(true));
    Entity clonedLead = new Entity("lead");
    foreach (var att in leadToClone.Attributes){
        //The fields I don't want to copy
        if (att.Key != "ownerid" && att.Key != "statuscode" && att.Key != "statecode" && att.Key!= "leadid" && att.Key != "address1_addressid" && att.Key != "address2_addressid"){
                clonedLead[att.Key] = att.Value;
            }
        }
    service.Create(clonedLead);

  • Suggested answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 11,321 on at
    RE: I am creating a clone of records but getting an error.

    Hi,

    You need to remove GUID of the cloned object.

    For example:

    entity.Attributes.Remove("msdyn_workorderid");

    Then, it should create a unique ID for you (when you create the record).

  • Ravi Fulani Profile Picture
    Ravi Fulani 814 on at
    RE: I am creating a clone of records but getting an error.

    Hi There,

    From JS I am calling Action... below is the code of my action.

    Below is the code

                       string entityID = (string)context.InputParameters["SelectedId"];                  

                       Entity entity = service.Retrieve("msdyn_workorder", new Guid(entityID), new ColumnSet(true));                  

                       entity.EntityState = null;                  

                       entity.Attributes.Remove(entity.LogicalName + "id");

                       entity.Attributes.Remove("msdyn_name");

                         entity.Id = Guid.NewGuid();

                     trace.Trace(entity.Id.ToString());

                     Guid createdWorkOrderID = service.Create(entity);

    without this line-> entity.Id = Guid.NewGuid();  my code is not running. and it is suggested that we should not create our own GUID as that impacts the system performance.

    service.Create(entity); is the line which will create GUID in system automatically once record get created.

  • meelamri Profile Picture
    meelamri 13,206 User Group Leader on at
    RE: I am creating a clone of records but getting an error.

    Hi,

    Would you please share your code? 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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans