web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Clone Records In crm 365 using workflow

(0) ShareShare
ReportReport
Posted on by 5,514

I cloned CRM records using workflow in crm

 7271.image_5F00_thumb.png

image_5F00_thumb1.png

question here is how can i clone the subgrid like i have Booking forecast and sales team

bforecast.PNG

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    SN Dwivedi Profile Picture
    20 on at
    RE: Clone Records In crm 365 using workflow

    I wanted to use the Javascript to clone the record and open that in new window. found so many articles but none of them were explaining much. To achieve this thing using Javascript you can follow this article.

    blog.infobitsoft.com/.../how-to-create-clone-record-using-Javascript-in-MS-Dynamics-365.html

  • Community Member Profile Picture
    on at
    RE: Clone Records In crm 365 using workflow

    HI Frances,

    I went thro the post and would like to use this but I am unable to download the zipfile also I am on version 8.2. Can you help me?

  • sandeepc Profile Picture
    5,514 on at
    RE: Clone Records In crm 365 using workflow

    0066.clone.PNG

  • sandeepc Profile Picture
    5,514 on at
    RE: Clone Records In crm 365 using workflow

    even though it is showing error it still creating an copy which means an clone function working fine

  • sandeepc Profile Picture
    5,514 on at
    RE: Clone Records In crm 365 using workflow

    pdp.PNG stiil the same error

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Clone Records In crm 365 using workflow

    Hi sandeepc,

    have you got some news about duplicated key error?

    Please let me know.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Clone Records In crm 365 using workflow

    Hi sandeepc ,

    looking forward to hear from you.

    Thanks

    Francesco

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • sandeepc Profile Picture
    5,514 on at
    RE: Clone Records In crm 365 using workflow

    will work on it and come back to you thanks

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Clone Records In crm 365 using workflow

    sandeepc ,

    here is the code:

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
    using System;
    using System.Activities;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace FP.Workflow
    {
        public class CloneEntity : CodeActivity
        {
            // ARGUMENTS //
    
            [Input("Parent record: Skip attribute name list")]
            [Default("entityid,entitycode,statecode,statuscode")]
            public InArgument<string> INPUT_MainEntity_SkipAttributeNameList { get; set; }
    
            [Input("Child record: Entity name")]
            public InArgument<string> INPUT_ChildEntity_EntityName { get; set; }
    
            [Input("Child record: Parent lookup attribute name")]
            public InArgument<string> INPUT_ChildEntity_ParentLookupAttributeName { get; set; }
    
            [Input("Child record: Skip attribute name list")]
            [Default("entityid,entitycode,statecode,statuscode")]
            public InArgument<string> INPUT_ChildEntity_SkipAttributeNameList { get; set; }
    
    
            // EXECUTE //
    
            protected override void Execute(CodeActivityContext activitycontext)
            {
                IWorkflowContext workflowcontext = activitycontext.GetExtension<IWorkflowContext>();
                ITracingService trace = activitycontext.GetExtension<ITracingService>();
                IOrganizationServiceFactory servicefactory = activitycontext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = servicefactory.CreateOrganizationService(null);
    
                Guid userid = workflowcontext.UserId;
                Guid entityid = workflowcontext.PrimaryEntityId;
                string entityname = workflowcontext.PrimaryEntityName;
    
                string parententity_skipattributenamelist = INPUT_MainEntity_SkipAttributeNameList.Get<string>(activitycontext);
                string childentity_entityname = INPUT_ChildEntity_EntityName.Get<string>(activitycontext);
                string childentity_parentlookupattributename = INPUT_ChildEntity_ParentLookupAttributeName.Get<string>(activitycontext);
                string childentity_skipattributenamelist = INPUT_ChildEntity_SkipAttributeNameList.Get<string>(activitycontext);
    
                Main(service, entityname, entityid, parententity_skipattributenamelist, childentity_entityname, childentity_parentlookupattributename, childentity_skipattributenamelist);
            }
    
    
            // MAIN //
    
            public static void Main(IOrganizationService service, string entityname, Guid entityid, string parententity_skipattributenamelist, string childentity_entityname, string childentity_parentlookupattributename, string childentity_skipattributenamelist)
            {
                Entity parententity = service.Retrieve(entityname, entityid, new ColumnSet(true));
    
                if (parententity != null)
                {
                    Entity parententityclone = Clone(service, parententity, parententity_skipattributenamelist);
                    Guid parententitycloneid = service.Create(parententityclone);
                    parententityclone = service.Retrieve(parententityclone.LogicalName, parententitycloneid, new ColumnSet(true));
    
                    if (!string.IsNullOrEmpty(childentity_entityname) && !string.IsNullOrEmpty(childentity_parentlookupattributename))
                    {
                        List<Entity> childentitylist = RetrieveList(service, childentity_entityname, childentity_parentlookupattributename, parententity.ToEntityReference());
    
                        foreach (Entity childentity in childentitylist)
                        {
                            Entity childentityclone = Clone(service, childentity, childentity_skipattributenamelist);
                            childentityclone[childentity_parentlookupattributename] = parententityclone.ToEntityReference();
                            service.Create(childentityclone);
                        }
                    }
                }
            }
    
    
            // UTILS //
    
            protected static List<Entity> RetrieveList(IOrganizationService service, string entityName, string parentLookupAttributeName, EntityReference parentEntityReference)
            {
                QueryByAttribute query = new QueryByAttribute(entityName);
                query.ColumnSet = new ColumnSet(true);
                query.Attributes.AddRange(parentLookupAttributeName);
                query.Values.AddRange(parentEntityReference.Id);
                List<Entity> entitylist = service.RetrieveMultiple(query).Entities.ToList();
                return entitylist;
            }
    
            protected static Entity Clone(IOrganizationService service, Entity sourceEntity, string skipAttributeNameStringList)
            {
                Entity targetentity = new Entity(sourceEntity.LogicalName);
                List<string> skipattributenamelist = skipAttributeNameStringList.Split(',').ToList();
    
                foreach (KeyValuePair<string, object> attr in sourceEntity.Attributes)
                {
                    if (skipattributenamelist.Contains(attr.Key))
                        continue;
                    targetentity[attr.Key] = attr.Value;
                }
                return targetentity;
            }
        }
    }

    If you want you could call it also from console, like:

    Entity order1ent = svc.Retrieve("salesorder", salesorderid, new ColumnSet(true));
    
    FP.Workflow.CloneEntity.Main(svc, 
        order1ent.LogicalName, order1ent.Id, 
        "salesorderid,ordernumber,statecode,statuscode", "salesorderdetail", "salesorderid", "salesorderdetailid,statecode,statuscode");

    Please let me know.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • sandeepc Profile Picture
    5,514 on at
    RE: Clone Records In crm 365 using workflow

    217737.Capture.PNG

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans