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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

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

(0) ShareShare
ReportReport
Posted on by 836

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.

I have the same question (0)
  • meelamri Profile Picture
    13,218 User Group Leader on at

    Hi,

    Would you please share your code? Thanks !

  • Ravi Fulani Profile Picture
    836 on at

    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.

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at

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

  • Suggested answer
    meelamri Profile Picture
    13,218 User Group Leader on at

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

  • Ravi Fulani Profile Picture
    836 on at

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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Pallavi Phade Profile Picture

Pallavi Phade 102 Super User 2026 Season 1

#2
ManoVerse Profile Picture

ManoVerse 58 Super User 2026 Season 1

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 55 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans