April 15, 2010 7:38 AM
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..
Subscribe to get the latest posts sent to your email.
Posted by Nishant Rana
Categories: CRM, Microsoft Dynamics CRM
Mobile Site | Full Site
Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.
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
By Jhelumi on May 5, 2010 at 9:52 AM
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
By Nishant Rana on May 5, 2010 at 10:01 AM
Could you please send me all the files related to this i mean the ISV & aspx page code
Thanks
Sara
LikeLike
By Sara on September 30, 2010 at 3:41 PM
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
By Jhelumi on May 5, 2010 at 10:09 AM
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
By Jhelumi on May 6, 2010 at 9:42 AM
Could you please send me the files you get 🙂
Thanks
Sara
LikeLike
By Sara on September 30, 2010 at 3:31 PM
[…] notificationsArea.style.display = 'block'; } Reference:Nishant Rana's Weblog: Clone Records in CRM Jeremy Winchell: Cloning Records In Microsoft CRM 4.0 Using AJAX Controls CodePlex: MSCRM4 Web […]
LikeLike
By Clone Record in CRM using CrmServiceToolkit (JavaScript) - XRM Notebook - Thuta's CRM Blog on June 21, 2010 at 4:35 PM
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
By Elisa on August 24, 2010 at 7:18 PM
Excellent article – I was trying to do the same, but hadn’t realised about ‘order number’ – thanks!
LikeLike
By Peter Mills on October 8, 2010 at 8:26 AM
Hi, may i have the code as well? thanks
LikeLike
By hanna on November 3, 2010 at 3:15 PM
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
By invest liberty reserve on July 7, 2011 at 9:47 AM
Hi,
TargetCreateSalesOrder is not available in CRM 2013
can you do this for CRM 2013 ?
LikeLike
By Chirag on September 1, 2014 at 4:07 PM
[…] Clone Records in CRM […]
LikeLike
By Create Clone In MS CRM – CRM Practice on March 18, 2017 at 3:50 PM