How to do it...

  1. Add a reference to the Microsoft.CrmSdk.Workflow NuGet package.
  2. Create a new folder in your Visual Studio solution called CustomWorkflow.
  3. Create a new abstract class called BaseCustomWorkflow that inherits from CodeActivity:
public abstract class BaseCustomWorkflow : CodeActivity 
  1. Add the following using statements:
using Microsoft.Xrm.Sdk; 
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Workflow;
using Packt.Xrm.Extensions.DataAccessLayer;
using System.Activities;
  1. Add the following protected instance variables:
protected IOrganizationService OrganizationService; 
protected ICustomTracingService CustomTracingService;
protected DataAccessLayerFactory DataAccessLayerFactory;
protected IWorkflowContext WorkflowContext;
  1. Add the following abstract method:
public abstract void PostExecute(CodeActivityContext executionContext); 
  1. Add the following Execute method:
protected override void Execute(CodeActivityContext
executionContext)
{
var serviceFactory =
executionContext.GetExtension<IOrganizationServiceFactory>();
var tracingService =
executionContext.GetExtension<ITracingService>();
WorkflowContext =
executionContext.GetExtension<IWorkflowContext>();
OrganizationService =
serviceFactory.CreateOrganizationService(WorkflowContext.UserId);
try
{
using (DataAccessLayerFactory = new
DataAccessLayerFactory(OrganizationService, tracingService))
{
CustomTracingService =
DataAccessLayerFactory.GetTracingService();

PostExecute(executionContext);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred
in the UpdateActivities plug-in.", ex);
}
catch (Exception ex)
{
CustomTracingService.Trace("FollowupPlugin: {0}",
ex.ToString());
throw;
}
}
  1. In the same CustomWorkflow folder, create a new public class called UpdateEmailsWorkflowActivity that inherits from BaseCustomWorkflow.
  2. Add the following using statements:
using System.Activities; 
using Microsoft.Xrm.Sdk.Workflow;
using Packt.Xrm.Extensions.BusinessLogic;
  1. Add the following workflow in the argument:
[Input("Account Guid")] 
[Default("00000000-0000-0000-0000-000000000000")]
public InArgument<string> AccountGuid { get; set; }
  1. Add the following PostExecute method:
public override void PostExecute(CodeActivityContext 
executionContext)
{
var input = AccountGuid.Get<string>(executionContext);
var accountId = input ==
Guid.Empty.ToString().Trim('{').Trim('}') ?
WorkflowContext.PrimaryEntityId : Guid.Parse(input);
CustomTracingService.Trace("Input value {0}", input);

var updateEmailLogic = new
UpdateEmailLogic(DataAccessLayerFactory.GetEmailDataAccessLayer(),
CustomTracingService);
updateEmailLogic.UpdateAccountsEmails(accountId);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.188.16