protected override void Execute(CodeActivityContext executionContext) { ITracingService tracer = executionContext.GetExtension <ITracingService>(); tracer.Trace("Inicialize context"); IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); //Create an Organization Service tracer.Trace("Inicialize serviceFactory"); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); tracer.Trace("Inicialize orgService"); IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.InitiatingUserId); //Registered Step Guid var pluginStepGuid = Guid.Empty; try { //Plugin Step object tracer.Trace("Inicialize pluginStep"); CrmPluginStep pluginStep = new CrmPluginStep(); pluginStep.PrimaryEntity = PrimaryEntity.Get <string>(executionContext); tracer.Trace("PrimaryEntity: " + pluginStep.PrimaryEntity); pluginStep.PluginAssemblyName = AssemblyName.Get <string>(executionContext); tracer.Trace("PluginAssemblyName: " + pluginStep.PluginAssemblyName); pluginStep.EventHandler = EventHandler.Get <string>(executionContext); tracer.Trace("EventHandler: " + pluginStep.EventHandler); pluginStep.Mode = Mode.Get <int>(executionContext); tracer.Trace("Mode: " + pluginStep.Mode); pluginStep.Rank = Rank.Get <int>(executionContext); tracer.Trace("Rank: " + pluginStep.Rank); pluginStep.FilteringAttributes = FilteringAttributes.Get <string>(executionContext); tracer.Trace("FilteringAttributes: " + pluginStep.FilteringAttributes); pluginStep.InvocationSource = InvocationSource.Get <int>(executionContext); tracer.Trace("InvocationSource: " + pluginStep.InvocationSource); pluginStep.Stage = Stage.Get <int>(executionContext); tracer.Trace("Stage: " + pluginStep.Stage); pluginStep.Deployment = Deployment.Get <int>(executionContext); tracer.Trace("Deployment: " + pluginStep.Deployment); pluginStep.Message = Message.Get <string>(executionContext); tracer.Trace("Message: " + pluginStep.Message); pluginStep.Name = pluginStep.EventHandler + ": " + pluginStep.Message + " of " + pluginStep.PrimaryEntity; tracer.Trace("Name: " + pluginStep.Name); pluginStep.tracer = tracer; tracer.Trace("--- Register Plugin Step --- "); //Register Step pluginStepGuid = pluginStep.RegisterPluginStep(ref orgService); tracer.Trace("pluginStepGuid: " + pluginStepGuid); PluginStepGuid.Set(executionContext, pluginStepGuid.ToString()); } catch (Exception e) { throw new InvalidPluginExecutionException("(RegisterPluginStep) Error! " + 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>(); ITracingService t = tracingService; if (tracingService == null) { throw new InvalidPluginExecutionException("Failed to retrieve tracing service."); } tracingService.Trace("Entered DeleteEntity.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("DeleteEntity.Execute(), Correlation Id: {0}, Initiating User: {1}", context.CorrelationId, context.InitiatingUserId); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try { // Courtesy of Gonzalo Ruiz https://crm2011workflowutils.codeplex.com/ if (PrimaryEntity.Get(executionContext)) { t.Trace("Deleting process primary entity"); service.Delete(context.PrimaryEntityName, context.PrimaryEntityId); } else { string relatedAttribute = RelatedAttributeName.Get(executionContext); if (string.IsNullOrEmpty(relatedAttribute)) { Helpers.Throw("If deleting related entity, related attribute name must be specified in the process delete step configuration"); } // Retrieve primary entity with the required attribute t.Trace("Retrieving process primary entity"); Entity primaryEntity = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(relatedAttribute)); if (primaryEntity.Contains(relatedAttribute)) { EntityReference reference = primaryEntity[relatedAttribute] as EntityReference; if (reference == null) { Helpers.Throw(string.Format("The attribute {0} on entity {1} is expected to be of EntityReference type", relatedAttribute, context.PrimaryEntityName)); } t.Trace("Deleting entity related to primary entity by attribute " + relatedAttribute); service.Delete(reference.LogicalName, reference.Id); } } } catch (FaultException <OrganizationServiceFault> e) { tracingService.Trace("Exception: {0}", e.ToString()); // Handle the exception. throw; } tracingService.Trace("Exiting DeleteEntity.Execute(), Correlation Id: {0}", context.CorrelationId); }