{"id":3852,"date":"2020-06-28T12:26:43","date_gmt":"2020-06-28T12:26:43","guid":{"rendered":"https:\/\/himbap.com\/blog\/?p=3852"},"modified":"2020-06-28T12:27:18","modified_gmt":"2020-06-28T12:27:18","slug":"resend-pending-emails-after-editing","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=3852","title":{"rendered":"Resend Pending Emails After Editing"},"content":{"rendered":"<p><strong>Problem<\/strong><br \/>\nRecently we got one requirement to resend some pending emails after changing from, In this article I am going to share steps to the same.<\/p>\n<p><strong>Solution<\/strong><br \/>\nPending emails are read only so we can&#8217;t edit them, incase you want to perform any changes to these emails you can use following below steps.<\/p>\n<p>1. Change Email stage to draft so that we can edit it.<br \/>\n2. Update Emails.<br \/>\n3. Send email using Code.<\/p>\n<p>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 <a href=\"https:\/\/himbap.com\/blog\/?p=780\">here<\/a>.<\/p>\n<p>We need to use following code in our workflow assembly<\/p>\n<p> public class ResendPendingEmails: CodeActivity {<br \/>\n  [RequiredArgument]<br \/>\n  [Input(&quot;SourceEmail&quot;)]<br \/>\n  [ReferenceTarget(&quot;email&quot;)]<br \/>\n  public InArgument &lt; EntityReference &gt; SourceEmail { get; set; }<\/p>\n<p>  protected override void Execute(CodeActivityContext executionContext) {<br \/>\n   \/\/Create the tracing service<br \/>\n   ITracingService tracingService = executionContext.GetExtension &lt; ITracingService &gt; ();<br \/>\n   \/\/Create the context<br \/>\n   IWorkflowContext context = executionContext.GetExtension &lt; IWorkflowContext &gt; ();<br \/>\n   IOrganizationServiceFactory serviceFactory = executionContext.GetExtension &lt; IOrganizationServiceFactory &gt; ();<br \/>\n   IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);<\/p>\n<p>   EntityReference email = SourceEmail.Get(executionContext);<\/p>\n<p>   SendEmailRequest req = new SendEmailRequest();<br \/>\n   req.EmailId = email.Id;<br \/>\n   req.TrackingToken = &quot;&quot;;<br \/>\n   req.IssueSend = true;<br \/>\n   SendEmailResponse res = (SendEmailResponse) service.Execute(req);<\/p>\n<p>  }<br \/>\n }<\/p>\n<p>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:<\/p>\n<p>1. First add step to change current email status to draft like below<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-3853 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail-300x57.jpg\" alt=\"resendemail\" width=\"300\" height=\"57\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail-300x57.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail.jpg 525w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n2. Add Update step where we can edit our existing email, we want to change From lookup.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail1.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3854 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail1-300x32.jpg\" alt=\"resendemail1\" width=\"300\" height=\"32\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail1-300x32.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail1.jpg 512w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n3. Third step where we need to use our custom workflow assembly and need to pass current email record to our input parameter like following:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail2.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-3855 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail2-300x185.jpg\" alt=\"resendemail2\" width=\"300\" height=\"185\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail2-300x185.jpg 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail2-1024x631.jpg 1024w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail2-624x384.jpg 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2020\/06\/resendemail2.jpg 1081w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Activate your workflow and run this on demand workflow on the pending email records, it will modify emails and will resend it.<\/p>\n<p>So using above methods we can edit and resend pending emails.<\/p>\n<p>Hope it will help someone!<br \/>\n<strong>Keep learning, Keep sharing !!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=3852\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,402,522,20],"tags":[781,780,779,782],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3852"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3852"}],"version-history":[{"count":4,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3852\/revisions"}],"predecessor-version":[{"id":3859,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3852\/revisions\/3859"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}