Resend Pending Emails After Editing

Introduction


Recently, we got one requirement to resend some pending emails after modification. In this article, I am going to share the solution steps.
 

Solution


Pending emails are read only so we can’t edit them. In case you want to perform any changes to these emails, you can use the following below steps.
  • Change Email stage to draft so that we can edit it.
  • Update Emails.
  • Send email using Code.
We are going to use workflow to perform the 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 to my old post here.
 
We need to use following code in our workflow assembly:
  1. public class ResendPendingEmails: CodeActivity {  
  2.     [RequiredArgument]  
  3.     [Input("SourceEmail")]  
  4.     [ReferenceTarget("email")]  
  5.     public InArgument < EntityReference > SourceEmail {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     protected override void Execute(CodeActivityContext executionContext) {  
  10.         //Create the tracing service    
  11.         ITracingService tracingService = executionContext.GetExtension < ITracingService > ();  
  12.         //Create the context    
  13.         IWorkflowContext context = executionContext.GetExtension < IWorkflowContext > ();  
  14.         IOrganizationServiceFactory serviceFactory = executionContext.GetExtension < IOrganizationServiceFactory > ();  
  15.         IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);  
  16.         EntityReference email = SourceEmail.Get(executionContext);  
  17.         SendEmailRequest req = new SendEmailRequest();  
  18.         req.EmailId = email.Id;  
  19.         req.TrackingToken = "";  
  20.         req.IssueSend = true;  
  21.         SendEmailResponse res = (SendEmailResponse) service.Execute(req);  
  22.     }  
  23. }   
In the above code, we added one input parameter to our workflow assembly, which will take current email record as a parameter. We are just sending the 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. Next, we need to create on demand workflow on the Email entity and use the following steps.
 
Step 1
 
First add a step to change the current email status to draft, like shown below:
 
Resend Pending Emails After Editing
 
Step 2
 
Add an Update step where we can edit our existing email. We want to change From lookup.
 
Resend Pending Emails After Editing
 
Step 3
 
The third step is where we need to use our custom workflow assembly and pass the current email record to our input parameter, as shown in the following:
 
Resend Pending Emails After Editing
 
Activate your workflow and run this on demand workflow on the pending email records. It will modify emails and will resend it.
 
So using the above methods, we can edit and resend pending emails.
 
I hope it will help someone!

Keep learning, Keep sharing!!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.