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); } }
/// <summary> /// Executes the workflow activity. /// </summary> /// <param name="executionContext">The execution context.</param> protected override void Execute(CodeActivityContext executionContext) { // Create the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); if (tracingService == null) { throw new InvalidPluginExecutionException("Failed to retrieve tracing service."); } tracingService.Trace("Entered GetRecordGuid.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}", executionContext.ActivityInstanceId, executionContext.WorkflowInstanceId); // Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); if (context == null) { throw new InvalidPluginExecutionException("Failed to retrieve workflow context."); } tracingService.Trace("GetRecordGuid.Execute(), Correlation Id: {0}, Initiating User: {1}", context.CorrelationId, context.InitiatingUserId); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { var entityReference = new DynamicUrlParser(RecordUrl.Get <string>(executionContext)); RecordGuid.Set(executionContext, entityReference.Id.ToString()); EntityLogicalName.Set(executionContext, entityReference.GetEntityLogicalName(service)); } catch (FaultException <OrganizationServiceFault> e) { tracingService.Trace("Exception: {0}", e.ToString()); // Handle the exception. throw; } tracingService.Trace("Exiting GetRecordGuid.Execute(), Correlation Id: {0}", context.CorrelationId); }
protected override void Execute(CodeActivityContext codeActivityContext) { // Set up ITracingService, IOrganizationService. ITracingService tracingService = codeActivityContext.GetExtension <ITracingService>(); IWorkflowContext context = codeActivityContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = codeActivityContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // Read InArguments. string recordUrl = RecordUrl.Get <string>(codeActivityContext); string appName = AppName.Get(codeActivityContext); // Create URL. string newRecordUrl = GenerateUrlForApp(service, recordUrl, appName); // Set OutArgument. NewRecordUrl.Set(codeActivityContext, newRecordUrl); }
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 noteToMove = NoteToMove.Get(context); if (noteToMove == null) { throw new ArgumentNullException("Note cannot be null"); } string recordUrl = RecordUrl.Get <string>(context); var dup = new DynamicUrlParser(recordUrl); string newEntityLogical = dup.GetEntityLogicalName(localContext.OrganizationService); Entity note = GetNote(localContext.OrganizationService, noteToMove.Id); if (note.GetAttributeValue <EntityReference>("objectid").Id == dup.Id && note.GetAttributeValue <EntityReference>("objectid").LogicalName == newEntityLogical) { WasNoteMoved.Set(context, false); return; } Entity updateNote = new Entity("annotation") { Id = noteToMove.Id, ["objectid"] = new EntityReference(newEntityLogical, dup.Id) }; localContext.OrganizationService.Update(updateNote); WasNoteMoved.Set(context, true); }
protected override void ExecuteCrmWorkFlowActivity(CodeActivityContext context, LocalWorkflowContext localContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (localContext == null) { throw new ArgumentNullException(nameof(localContext)); } var recordUrl = RecordUrl.Get <string>(context); var dup = new DynamicUrlParser(recordUrl); var foundNote = GetNote(localContext.OrganizationService, dup.Id); FoundNote.Set(context, foundNote); }
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 template = Template.Get(context); string recordUrl = RecordUrl.Get(context); var dup = new DynamicUrlParser(recordUrl); string logicalName = dup.GetEntityLogicalName(localContext.OrganizationService); Entity email = GenerateTemplate(localContext.OrganizationService, template.Id, dup.Id, logicalName); if (email == null) { throw new InvalidPluginExecutionException("Unexpected error creating template"); } string subject = null; bool hasSubject = email.Attributes.TryGetValue("subject", out object objSubject); if (hasSubject) { subject = objSubject.ToString(); } TemplateSubject.Set(context, subject); string description = null; bool hasDescription = email.Attributes.TryGetValue("description", out object objDescription); if (hasDescription) { description = objDescription.ToString(); } TemplateDescription.Set(context, description); }
protected override void Execute(CodeActivityContext executionContext) { #region "Load CRM Service from context" Common objCommon = new Common(executionContext); objCommon.tracingService.Trace("Load CRM Service from context --- OK"); IWorkflowContext workflowContext = executionContext.GetExtension <IWorkflowContext>(); #endregion #region "Read Parameters" string recordUrl = RecordUrl.Get(executionContext); string lookupFieldName = LookupFieldName.Get(executionContext); objCommon.tracingService.Trace("Inputs -- RecordUrl: " + recordUrl + " | LookupFieldName: " + lookupFieldName); #endregion "Read Parameters" #region "Set Lookup Value from Record URL" try { // Get the entity reference using the URL Parser DynamicUrlParser urlParser = new DynamicUrlParser(recordUrl); EntityReference entityRef = urlParser.ToEntityReference(objCommon.service); // Get details of the primary entity in the workflow context string primaryEntityName = workflowContext.PrimaryEntityName; Guid primaryEntityId = workflowContext.PrimaryEntityId; // Retrieve the current record with the lookup field ColumnSet columns = new ColumnSet(new string[] { lookupFieldName }); Entity recordToUpdate = objCommon.service.Retrieve(primaryEntityName, primaryEntityId, columns); objCommon.tracingService.Trace("PrimaryEntityName: " + primaryEntityName + " | PrimaryEntityId: " + primaryEntityId); // Set the lookup field to our entity ref recordToUpdate[lookupFieldName] = entityRef; // Update the record objCommon.service.Update(recordToUpdate); } catch (Exception e) { throw new InvalidPluginExecutionException("Error updating lookup field. " + e.ToString()); } #endregion "Set Lookup Value from Record URL" }
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 noteToMove = NoteToMove.Get(executionContext); string recordUrl = RecordUrl.Get <string>(executionContext); var dup = new DynamicUrlParser(recordUrl); string newEntityLogical = dup.GetEntityLogicalName(service); Entity note = GetNote(service, noteToMove.Id); if (note.GetAttributeValue <EntityReference>("objectid").Id == dup.Id && note.GetAttributeValue <EntityReference>("objectid").LogicalName == newEntityLogical) { WasNoteMoved.Set(executionContext, false); return; } Entity updateNote = new Entity("annotation"); updateNote.Id = noteToMove.Id; updateNote["objectid"] = new EntityReference(newEntityLogical, dup.Id); service.Update(updateNote); WasNoteMoved.Set(executionContext, true); } catch (Exception e) { throw new InvalidPluginExecutionException(e.Message); } }
protected override void Execute(CodeActivityContext executionContext) { // Extract the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); try { //Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // Don't Really Care about which entity this is DynamicUrlParser parser = new DynamicUrlParser(); EntityReference primaryEntity = parser.ConvertToEntityReference(service, RecordUrl.Get <string>(executionContext)); string addressLine1 = Street1.Get <string>(executionContext); string addressLine2 = Street2.Get <string>(executionContext); string city = City.Get <string>(executionContext); string stateOrProvince = StateOrProvince.Get <string>(executionContext); string zipCode = ZipPostalCode.Get <string>(executionContext); string countryName = Country.Get <string>(executionContext); string rc = GenerateAddress(addressLine1, addressLine2, city, stateOrProvince, zipCode, countryName); FullAddress.Set(executionContext, rc); } catch (FaultException <OrganizationServiceFault> ex) { throw new Exception("SBS.Workflow.SetStateChildRecords: " + ex.Message); } }
protected override void Execute(CodeActivityContext executionContext) { // Extract the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); try { //Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); DynamicUrlParser parser = new DynamicUrlParser(); EntityReference primaryEntity = parser.ConvertToEntityReference(service, RecordUrl.Get <string>(executionContext)); string relationshipName = RelationshipName.Get <string>(executionContext); Guid ownerId = Guid.Empty; string ownerType = string.Empty; EntityReference owner = SystemUser.Get <EntityReference>(executionContext); if (owner != null) { ownerId = owner.Id; ownerType = owner.LogicalName; } else { owner = Team.Get <EntityReference>(executionContext); if (owner != null) { ownerId = owner.Id; ownerType = owner.LogicalName; } } if (ownerId != Guid.Empty) { string childEntityName = null, childEntityAttribute = null; if (relationshipName.Contains(';')) { string[] relationshipNames = relationshipName.Split(';'); foreach (string rel in relationshipNames) { OneToManyRelationshipMetadata oneToNRelationship = RetrieveRelationshipInfo(service, primaryEntity, relationshipName); if (oneToNRelationship != null) { childEntityName = oneToNRelationship.ReferencingEntity; childEntityAttribute = oneToNRelationship.ReferencingAttribute; RetrieveAndUpdateRelatedRecords(service, primaryEntity, childEntityName, childEntityAttribute, ownerId, ownerType); } } } else { OneToManyRelationshipMetadata oneToNRelationship = RetrieveRelationshipInfo(service, primaryEntity, relationshipName); if (oneToNRelationship != null) { childEntityName = oneToNRelationship.ReferencingEntity; childEntityAttribute = oneToNRelationship.ReferencingAttribute; RetrieveAndUpdateRelatedRecords(service, primaryEntity, childEntityName, childEntityAttribute, ownerId, ownerType); } } } } catch (FaultException <OrganizationServiceFault> ex) { throw new Exception("XrmWorkflowTools.AssignChildRecords: " + ex.Message); } }
/// <summary> /// Executes the workflow activity. /// </summary> /// <param name="executionContext">The execution context.</param> protected override void Execute(CodeActivityContext executionContext) { // Create the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); if (tracingService == null) { throw new InvalidPluginExecutionException("Failed to retrieve tracing service."); } tracingService.Trace("Entered CreateDocumentLocation.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}", executionContext.ActivityInstanceId, executionContext.WorkflowInstanceId); // Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); if (context == null) { throw new InvalidPluginExecutionException("Failed to retrieve workflow context."); } tracingService.Trace("CreateDocumentLocation.Execute(), Correlation Id: {0}, Initiating User: {1}", context.CorrelationId, context.InitiatingUserId); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); IOrganizationService privService = serviceFactory.CreateOrganizationService(null); try { var url = RecordUrl.Get <string>(executionContext); var recordRef = new DynamicUrlParser(url).ToEntityReference(privService); string config = GetSecureConfigValue(privService, "PrivSharePointUser");; string[] user = config.Split(';'); // Create a new sharepoint service using the given priv sharepoint user credentials SPService spService = new SPService(user[0], user[1]); var docLocation = new DocumentLocationHelper(privService, spService); // Get the site passed into the workflow activity var siteId = Site.Get(executionContext); var site = (SharePointSite)service.Retrieve(SharePointSite.EntityLogicalName, siteId.Id, new ColumnSet("sharepointsiteid", "absoluteurl")); if (site != null) { // Set the name of the folder recordRef.Name = RecordFolderName.Get <string>(executionContext); var newLocation = docLocation.CreateDocumentLocation(site, DocumentLibraryName.Get(executionContext), recordRef); DocumentLocation.Set(executionContext, newLocation.ToEntityReference()); DocumentFolder.Set(executionContext, newLocation.AbsoluteURL); } else { DocumentLocation.Set(executionContext, null); } } catch (FaultException <OrganizationServiceFault> e) { tracingService.Trace("Exception: {0}", e.ToString()); // Handle the exception. throw; } tracingService.Trace("Exiting CreateDocumentLocation.Execute(), Correlation Id: {0}", context.CorrelationId); }
protected override void Execute(CodeActivityContext executionContext) { // Extract the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); try { //Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); DynamicUrlParser parser = new DynamicUrlParser(); EntityReference currentRecord = parser.ConvertToEntityReference(service, RecordUrl.Get <string>(executionContext)); Guid processId = RetrieveProcessId(service, Process.Get <string>(executionContext)); if (processId != Guid.Empty) { Guid stageId = RetrieveStageId(service, processId, ProcessStage.Get <string>(executionContext)); if (stageId != Guid.Empty) { UpdateStage(service, currentRecord, processId, stageId); } } } catch (FaultException <OrganizationServiceFault> ex) { throw new Exception("XrmWorkflowTools.ChangeProcessStage.Execute: " + ex.Message); } catch (System.Exception ex) { throw new Exception("XrmWorkflowTools.ChangeProcessStage.Execute: " + ex.Message); } }
protected override void Execute(CodeActivityContext executionContext) { // Extract the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); try { //Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); DynamicUrlParser parser = new DynamicUrlParser(); EntityReference refEntity = parser.ConvertToEntityReference(service, RecordUrl.Get <string>(executionContext)); EntityReference refTemplate = EmailTemplate.Get <EntityReference>(executionContext); Guid emailId = CreateTemplatedEmail(service, refEntity, refTemplate.Id); Entity email = service.Retrieve("email", emailId, new ColumnSet("description")); string description = email.Contains("description") ? email.GetAttributeValue <string>("description") : string.Empty; List <Entity> fromList = new List <Entity>(); EntityReference systemUser = Sender.Get <EntityReference>(executionContext); Entity user = new Entity("activityparty"); user["partyid"] = systemUser; fromList.Add(user); UpdateEmailMessage(service, emailId, refEntity, fromList); EmailMessage.Set(executionContext, new EntityReference("email", emailId)); } catch (FaultException <OrganizationServiceFault> ex) { throw new Exception("XrmWorkflowTools.CreateEmailFromTemplate: " + ex.Message); } catch (System.Exception ex) { throw new Exception("XrmWorkflowTools.CreateEmailFromTemplate: " + ex.Message); } }