- Add a reference to the Microsoft.CrmSdk.Workflow NuGet package.
- Create a new folder in your Visual Studio solution called CustomWorkflow.
- Create a new abstract class called BaseCustomWorkflow that inherits from CodeActivity:
public abstract class BaseCustomWorkflow : CodeActivity
- 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;
- Add the following protected instance variables:
protected IOrganizationService OrganizationService;
protected ICustomTracingService CustomTracingService;
protected DataAccessLayerFactory DataAccessLayerFactory;
protected IWorkflowContext WorkflowContext;
- Add the following abstract method:
public abstract void PostExecute(CodeActivityContext executionContext);
- 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;
}
}
- In the same CustomWorkflow folder, create a new public class called UpdateEmailsWorkflowActivity that inherits from BaseCustomWorkflow.
- Add the following using statements:
using System.Activities;
using Microsoft.Xrm.Sdk.Workflow;
using Packt.Xrm.Extensions.BusinessLogic;
- Add the following workflow in the argument:
[Input("Account Guid")]
[Default("00000000-0000-0000-0000-000000000000")]
public InArgument<string> AccountGuid { get; set; }
- 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);
}