Workflow

😇 Dynamics 365 workflow to send an email with notes attachment👆


Workflow Activity to get and set PartyList lookup fields  
Custom workflow Input-Output arguments for different attribute types
Workflow activity to send an email with attachments

A business has a requirement to send an email from Dynamics 365 with the attached document in the timeline section. That document basically needed to come from a notes attachment on a task record.
Click here to go to the main page

As you might have already tried existing OOB workflow functionality to achieve this. I also did the same tried multiple ways with OOB functionality but didn't find any way to achieve it.

Note: We can’t access an attachment from Notes in an OOB workflow so we need to write a custom workflow where we need to get an entity record attachment from an annotation entity and need to create an activitymimeattachment record and need to associate it with the email created. While creating the workflow we need to use a create step instead of sending the email so that we can refer to the email record id.

I also searched many blogs and came across an MS Community blog and Luckily there was one solution which was exactly matching with my requirement, to know more Click here

Here I'm sharing my own implementation. Below is the procedure to implement to achieve our requirement through custom workflow activity:

  1. Develop a custom workflow activity assembly to get an attachment.
  2. The code targets task activity. Its generic code, which can be used on any entity.
  3. Get all the note attachments from the timeline section.
  4. You need to create an email and do not use the workflow option to send an email.
Create a custom workflow activity. Create a class library project and add all the required assemblies to your project. Let’s rename our class file to SendEmailWithAttachement and use the following code:

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
 
namespace WorkflowEmailAttachment
{
    public class SendEmailWithAttachement : CodeActivity
    {
        [Input("SourceEmail")]
        [ReferenceTarget("email")]
        public InArgument<EntityReference> SourceEmail { get; set; }
 
        [Input("SendEmail")]
        public InArgument<Boolean> SendEmail { get; set; }
        protected override void Execute(CodeActivityContext executionContext)
        {
            // Get workflow context
            var tracingService = executionContext.GetExtension<ITracingService>();
            var context = executionContext.GetExtension<IWorkflowContext>();
            var serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            var service = serviceFactory.CreateOrganizationService(context.UserId);
 
            try
            {
                AddAttachmentToEmailRecord(service, context.PrimaryEntityId, SourceEmail.Get<EntityReference>(executionContext));
            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
private void AddAttachmentToEmailRecord(IOrganizationService service, Guid sourceId, EntityReference sourceEmailID)
        {
            Entity resultEntity = service.Retrieve("email", sourceEmailID.Id, new ColumnSet(true));
 
            QueryExpression queryNotes = new QueryExpression("annotation");
            queryNotes.ColumnSet = new ColumnSet(new string[] { "subject", "mimetype", "filename", "documentbody" });
            queryNotes.Criteria = new FilterExpression();
            queryNotes.Criteria.FilterOperator = LogicalOperator.And;
            queryNotes.Criteria.AddCondition(new ConditionExpression("objectid", ConditionOperator.Equal, sourceId));
            EntityCollection mimeCollection = service.RetrieveMultiple(queryNotes);
 
            foreach (var attachment in mimeCollection.Entities)
            {
                Entity emailAttachment = new Entity("activitymimeattachment");
                if (attachment.Contains("subject")) emailAttachment["subject"] = attachment.GetAttributeValue<string>("subject");
                if (attachment.Contains("filename")) emailAttachment["filename"] = attachment.GetAttributeValue<string>("filename");
                if (attachment.Contains("documentbody")) emailAttachment["body"] = attachment.GetAttributeValue<string>("documentbody");
if (attachment.Contains("mimetype")) emailAttachment["mimetype"] = attachment.GetAttributeValue<string>("mimetype");
                emailAttachment["objectid"] = new EntityReference("email", resultEntity.Id);
                emailAttachment["objecttypecode"] = "email";
                service.Create(emailAttachment);
            }
 
            SendEmailRequest sendRequest = new SendEmailRequest();
            sendRequest.EmailId = resultEntity.Id;
            sendRequest.TrackingToken = "";
            sendRequest.IssueSend = true;
            SendEmailResponse res = (SendEmailResponse)service.Execute(sendRequest);
        }
    }

}

--------------------------------------------------------------------------------

Build and register your assembly using the plug-in registration tool. Now we need to create our workflow to use our custom assembly step.

Navigate to Settings->Process to create a new process. Select Task in entity drop-down and workflow in the category.

Follow the below steps to configure your workflow

  • Add a step to create an email record that can be used in the custom step
  • Add our custom workflow steps from Add Step.
Create workflow a process as shown in the below image:














Click on Add Step -> Click on Create Records -> Select Email -> Set Properties:


Click on Add steps-> Select workflow which you registered in plugin registration tool (popped up here) -> Click on Set Properties -> Add details as shown:




Save & Activate the workflow -> The workflow looks like as shown below:






No comments:

Post a Comment