A client recently had issues implementing a workflow that would send a draft email. Under a normal scenario, you could choose to create a “Send Email” step within a workflow and the job would be done. But what if you have a number of draft emails that you want to push out at a later stage?

One option is, if your email configuration has been set to Email Router then you could issue a SetState Command and set the status to Pending Send with a Number of Attempts set to a value above 0. This would then be sent out next time the email router interrogated your email records. However, this method is not completely reliable where the email configuration has been set to Server Side Synchronisation.

Thankfully, there is an easy solution. It involves a little custom code, and registering this custom workflow to your CRM.

Step 1: Compile and create a Custom Workflow within Visual Studio.

using System;

using System.Activities;

using System.Collections.ObjectModel;

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Messages;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Workflow;

namespace Helper.SendEmailWF

{




public sealed class PushEmail : CodeActivity

{




protected override void Execute(CodeActivityContext executionContext)

{

//Create the tracing service

ITracingService tracingService = executionContext.GetExtension<ITracingService>();

//Create the context

IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

tracingService.Trace("Pushing Email");

// Variable Declaration

SendEmailRequest newSendEmailRequest = new SendEmailRequest();

GetTrackingTokenEmailRequest trackingTokenEmailRequest = new GetTrackingTokenEmailRequest();

GetTrackingTokenEmailResponse trackingTokenEmailResponse = null;

// retrieve Email Reference

EntityReference emailRef = EmailReference.Get(executionContext); ;

// configure Email Request

newSendEmailRequest.EmailId = emailRef.Id;

newSendEmailRequest.IssueSend = true;

// receive email tracking token

trackingTokenEmailResponse = (GetTrackingTokenEmailResponse)service.Execute(trackingTokenEmailRequest);

// setting email tracking token

newSendEmailRequest.TrackingToken = trackingTokenEmailResponse.TrackingToken;

// send request

service.Execute(newSendEmailRequest);

tracingService.Trace("Email has been send ");

}

[RequiredArgument]

[ReferenceTarget("email")]

[Input("Email Reference")]

//[Default("Test Account: {575A8B41-F8D7-4DCE-B2EA-3FFDE936AB1B}")]

public InArgument<EntityReference> EmailReference { get; set; }

}




}

Step 2: Register your workflow DLL to your CRM instance through the Plugin Registration Tool.

Step 3: Create a Workflow Process within Dynamics CRM. With the step as pictured below:

Set the Entity Reference for “Email Reference” as such

Now you are ready to test your workflow.

Summary

I hope that you have found this blog informative. If you have any suggestions or any tricky Dynamics issues you think should be included in future blogs, please drop me a message below.