protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (localContext == null) { throw new ArgumentNullException(nameof(localContext)); } EntityReference noteToCopy = NoteToCopy.Get(context); if (noteToCopy == null) { throw new ArgumentNullException("Note cannot be null"); } string recordUrl = RecordUrl.Get <string>(context); bool copyAttachment = CopyAttachment.Get(context); var dup = new DynamicUrlParser(recordUrl); string newEntityLogical = dup.GetEntityLogicalName(localContext.OrganizationService); Entity note = GetNote(localContext.OrganizationService, noteToCopy.Id); if (note.GetAttributeValue <EntityReference>("objectid").Id == dup.Id && note.GetAttributeValue <EntityReference>("objectid").LogicalName == newEntityLogical) { WasNoteCopied.Set(context, false); return; } Entity newNote = new Entity("annotation") { ["objectid"] = new EntityReference(newEntityLogical, dup.Id), ["notetext"] = note.GetAttributeValue <string>("notetext"), ["subject"] = note.GetAttributeValue <string>("subject") }; if (copyAttachment) { newNote["isdocument"] = note.GetAttributeValue <bool>("isdocument"); newNote["filename"] = note.GetAttributeValue <string>("filename"); newNote["filesize"] = note.GetAttributeValue <int>("filesize"); newNote["documentbody"] = note.GetAttributeValue <string>("documentbody"); } else { newNote["isdocument"] = false; } localContext.OrganizationService.Create(newNote); WasNoteCopied.Set(context, true); }
protected override void Execute(CodeActivityContext executionContext) { ITracingService tracer = executionContext.GetExtension <ITracingService>(); IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { EntityReference noteToCopy = NoteToCopy.Get(executionContext); string recordUrl = RecordUrl.Get <string>(executionContext); bool copyAttachment = CopyAttachment.Get(executionContext); var dup = new DynamicUrlParser(recordUrl); string newEntityLogical = dup.GetEntityLogicalName(service); Entity note = GetNote(service, noteToCopy.Id); if (note.GetAttributeValue <EntityReference>("objectid").Id == dup.Id && note.GetAttributeValue <EntityReference>("objectid").LogicalName == newEntityLogical) { WasNoteCopied.Set(executionContext, false); return; } Entity newNote = new Entity("annotation"); newNote["objectid"] = new EntityReference(newEntityLogical, dup.Id); newNote["notetext"] = note.GetAttributeValue <string>("notetext"); newNote["subject"] = note.GetAttributeValue <string>("subject"); if (copyAttachment) { newNote["isdocument"] = note.GetAttributeValue <bool>("isdocument"); newNote["filename"] = note.GetAttributeValue <string>("filename"); newNote["filesize"] = note.GetAttributeValue <int>("filesize"); newNote["documentbody"] = note.GetAttributeValue <string>("documentbody"); } else { newNote["isdocument"] = false; } service.Create(newNote); WasNoteCopied.Set(executionContext, true); } catch (Exception e) { throw new InvalidPluginExecutionException(e.Message); } }