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


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

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

13 thoughts on “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

  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

  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

  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

    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

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.