was just looking for different ways we can clone a record in CRM.
These are the different articles that i found :-
Cloning records through an ASP.NET page
Cloning records without any code by making use of self-referential relationship and mapping
http://rc.crm.dynamics.com/rc/regcont/en_us/op/articles/cloneRecords.aspx
Cloning records through workflow
http://www.dynamicscrmtrickbag.com/2009/08/29/clone-workflow/
Cloning records through JavaScript
http://ronaldlemmen.blogspot.com/2007/05/clone-record.html
And below is the code which i have used to clone Sales Order with all related sales order details
public void CloneSalesOrder(string salesOrderId, CrmService crmService)
{
Guid id = new Guid(salesOrderId);
// get all the attributes values using AllColumns
salesorder originalSalesOrder = (salesorder)crmService.
Retrieve(EntityName.salesorder.ToString(), id, new AllColumns());
salesorder cloneSalesOrder = new salesorder();
// assign the original sales order to the cloned one
cloneSalesOrder = originalSalesOrder;
cloneSalesOrder.name = "Cloned SalesOrder";
// set sales order id and order number as null
cloneSalesOrder.salesorderid = null;
cloneSalesOrder.ordernumber = null;
TargetCreateSalesOrder order = new TargetCreateSalesOrder();
order.SalesOrder = cloneSalesOrder;
CreateRequest request = new CreateRequest();
request.Target = order;
CreateResponse response = (CreateResponse)crmService.Execute(request);
// clone all the line items as well
CloneSalesOrderLineItems(salesOrderId, response.id.ToString(),
crmService);
}
private void CloneSalesOrderLineItems(string objPoId, string
objClonedPoId, CrmService service)
{
CrmService crmService = service;
//Retrieve all the sales order details from the original sales
//order record
ConditionExpression expression = new ConditionExpression();
expression.AttributeName = "salesorderid";
expression.Operator = ConditionOperator.Equal;
expression.Values = new string[] { objPoId.Trim() };
FilterExpression expression2 = new FilterExpression();
expression2.FilterOperator = LogicalOperator.And;
expression2.Conditions = new ConditionExpression[] { expression };
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.salesorderdetail.ToString();
query.ColumnSet = new AllColumns();
query.Criteria = expression2;
BusinessEntityCollection entitys = crmService.RetrieveMultiple(query);
if (entitys != null)
{
// one by one create sales order detail record and set its sales
//order id as the new cloned record id
foreach (salesorderdetail salesorderdetail in entitys.BusinessEntities)
{
salesorderdetail salesorderdetail2 = new salesorderdetail();
salesorderdetail2 = salesorderdetail;
Guid guid = new Guid(objClonedPoId);
salesorderdetail2.salesorderid = null;
salesorderdetail2.salesorderdetailid = null;
Lookup lookup = new Lookup();
lookup.Value = guid;
salesorderdetail2.salesorderid = lookup;
salesorderdetail2.salesorderid.Value = lookup.Value;
TargetCreateSalesOrderDetail detail = new
TargetCreateSalesOrderDetail();
detail.SalesOrderDetail = salesorderdetail2;
CreateRequest request = new CreateRequest();
request.Target = detail;
CreateResponse response = (CreateResponse)crmService.Execute(request);
}
}
}
Bye..
Discover more from Nishant Rana's Weblog
Subscribe to get the latest posts sent to your email.

Hi Nishant,
Could you pls explain how and where should I call this code to create a clone of an order.
Ideally I want a button either on Order Form to copy and existing(opened) order or from the view action bar so that the user can select an order from the view and create a copy of that order.
Thanks in advance
LikeLike
Hi,
What we have done is we have wrote a custom aspx page with the above code, having two buttons one for cloning po and other for closing the form.
We are opening that page from an isv.config button wherein we are passing the currents record’s guid as query string. And that guid than we are passing to the ClonesSalesOrder function.
So ideally you just need to pass the records guid to get it cloned.
Regards,
Nishant Rana
LikeLike
Could you please send me all the files related to this i mean the ISV & aspx page code
Thanks
Sara
LikeLike
Thanks for prompt reply.
Could you please share the ISV & aspx page code as well.
Secondly would this work from Outlook as well?
Regards
Jhelumi
LikeLike
Hi Nishant,
Thankyou very much for the files. I manage to get it working apart from one issue that the cloned order and order products does not get filled in with the custom attributes.
How can I force it to copy the custom attributes as well or can I specify the list of fields to be copied?
Thanks once again
Regards
Jhelumi
LikeLike
Could you please send me the files you get 🙂
Thanks
Sara
LikeLike
Could you please share the ISV & aspx page code with me as well.
Secondly with this code how would I force it to copy the custom attributes as well?
Thanks
LikeLike
Excellent article – I was trying to do the same, but hadn’t realised about ‘order number’ – thanks!
LikeLike
Hi, may i have the code as well? thanks
LikeLike
Your Article about Clone Records in CRM Nishant Rana's Weblog Really great visual appeal on this website , I’d value it 10 10.
LikeLike
Hi,
TargetCreateSalesOrder is not available in CRM 2013
can you do this for CRM 2013 ?
LikeLike