Clone Records in CRM

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

http://blogs.inetium.com/blogs/microsoftcrm/archive/2009/04/27/cloning-records-in-microsoft-crm-4-0-using-ajax-controls.aspx

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


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

13 thoughts on “Clone Records in CRM”

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

      Like

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

    Like

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

    Like

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

    Like

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

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s