Resend Pending Emails After Editing

Problem
Recently we got one requirement to resend some pending emails after changing from, In this article I am going to share steps to the same.

Solution
Pending emails are read only so we can’t edit them, incase you want to perform any changes to these emails you can use following below steps.

1. Change Email stage to draft so that we can edit it.
2. Update Emails.
3. Send email using Code.

We are going to use workflow to perform above actions so first we will be writing a custom workflow utility where we will be writing code to send email. If you are new to writing Custom workflow you can refer my old post here.

We need to use following code in our workflow assembly

public class ResendPendingEmails: CodeActivity {
[RequiredArgument]
[Input("SourceEmail")]
[ReferenceTarget("email")]
public InArgument < EntityReference > SourceEmail { get; set; }

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);

EntityReference email = SourceEmail.Get(executionContext);

SendEmailRequest req = new SendEmailRequest();
req.EmailId = email.Id;
req.TrackingToken = "";
req.IssueSend = true;
SendEmailResponse res = (SendEmailResponse) service.Execute(req);

}
}

In above code we have added one input parameter to our workflow assembly which will take current email record as parameter and we are just sending that emails using SendEmailRequest. Build your assembly and register it in your Dynamics 365 CE organization. Once our custom workflow assembly is registered in Dynamics 365 CE, we can use it in our workflow, so next we need to create on demand workflow on Email entity and need to use following steps:

1. First add step to change current email status to draft like below
resendemail
2. Add Update step where we can edit our existing email, we want to change From lookup.
resendemail1
3. Third step where we need to use our custom workflow assembly and need to pass current email record to our input parameter like following:

resendemail2

Activate your workflow and run this on demand workflow on the pending email records, it will modify emails and will resend it.

So using above methods we can edit and resend pending emails.

Hope it will help someone!
Keep learning, Keep sharing !!

One thought on “Resend Pending Emails After Editing

  1. Pingback: Resend Pending Emails After Editing - Microsoft Dynamics CRM Community

Leave a Reply

Your email address will not be published. Required fields are marked *