I cloned CRM records using workflow in crm
question here is how can i clone the subgrid like i have Booking forecast and sales team
*This post is locked for comments
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
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?
even though it is showing error it still creating an copy which means an clone function working fine
stiil the same error
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
Hi sandeepc ,
looking forward to hear from you.
Thanks
Francesco
will work on it and come back to you thanks
H 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");
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
HR-09070029-0 2
ED-30091530-0 1