Nishant Rana's Weblog


Home | Pages | Archives


Clone Records in CRM

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

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


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Posted by Nishant Rana

Categories: CRM, Microsoft Dynamics CRM

Tags: ,

13 Responses to “Clone Records in CRM”

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

    By Jhelumi on May 5, 2010 at 9:52 AM

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

    By Nishant Rana on May 5, 2010 at 10:01 AM

    1. Could you please send me all the files related to this i mean the ISV & aspx page code

      Thanks

      Sara

      Like

      By Sara on September 30, 2010 at 3:41 PM

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

    By Jhelumi on May 5, 2010 at 10:09 AM

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

    By Jhelumi on May 6, 2010 at 9:42 AM

    1. Could you please send me the files you get 🙂

      Thanks

      Sara

      Like

      By Sara on September 30, 2010 at 3:31 PM

  5. […]     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 […]

    Like

    By Clone Record in CRM using CrmServiceToolkit (JavaScript) - XRM Notebook - Thuta's CRM Blog on June 21, 2010 at 4:35 PM

    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

      By Elisa on August 24, 2010 at 7:18 PM

  6. Excellent article – I was trying to do the same, but hadn’t realised about ‘order number’ – thanks!

    Like

    By Peter Mills on October 8, 2010 at 8:26 AM

  7. Hi, may i have the code as well? thanks

    Like

    By hanna on November 3, 2010 at 3:15 PM

  8. Your Article about Clone Records in CRM Nishant Rana's Weblog Really great visual appeal on this website , I’d value it 10 10.

    Like

    By invest liberty reserve on July 7, 2011 at 9:47 AM

  9. Hi,

    TargetCreateSalesOrder is not available in CRM 2013

    can you do this for CRM 2013 ?

    Like

    By Chirag on September 1, 2014 at 4:07 PM

  10. […] Clone Records in CRM […]

    Like

    By Create Clone In MS CRM – CRM Practice on March 18, 2017 at 3:50 PM

Leave a Reply



Mobile Site | Full Site


Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.