Sending Email Attachment From Entity Notes

If you are looking for sending email with an entity record attachment from Notes then this article is going to help you to implement this requirement. We will explain here how to send email using a workflow with an attachment from Notes regarding an entity. Let’s consider a business scenario where we have implemented a customer approval process and as soon as the customer approves we need to send him notification with an attachment in notes, this attachment contains the details about his account.

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.

So mainly we need to use the following procedure to implement our requirements:

  • Develop a custom workflow assembly to get an attachment.
  • Create a workflow and use a custom workflow assembly step to send the email.

Let’s create a custom workflow. If you are new to custom workflow assemblies then please refer to our previous article to create a custom workflow. Create a class library project and add the required assemblies to your project. Let’s rename our class file to SendEmailWithAttachement and use the following code:

namespace EmailAttachment

{

    public class SendEmailWithAttachement : CodeActivity

    { 

        #region Variable declaration

        [Input("TargetEmail")]

        [ReferenceTarget("email")]

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

        #endregion

        #region Execute Function

        protected override void Execute(CodeActivityContext executionContext)

        {

            // Get context

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

            //Create service factory object

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

 

            // Create Organization service

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

 

            // Get the target entity from the context

            Entity AccountEntity = (Entity)service.Retrieve("syn_trialrequest", context.PrimaryEntityId, new ColumnSet(new string[] { "accountid" }));

 

            AddAttachmentToEmail(service, AccountEntity.Id, TargetEmail.Get<EntityReference>(executionContext));

        }

        #endregion

        private void AddAttachmentToEmail(IOrganizationService service, Guid AccountEntityID, EntityReference TargetEmailID)

        {

            //Get email details

            Entity _ResultEntity = service.Retrieve("email", TargetEmailID.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, AccountEntityID));

 

            EntityCollection _MimeCollection = service.RetrieveMultiple(_QueryNotes);

 

            if (_MimeCollection.Entities.Count > 0)

            {

                Entity _NotesAttachment = _MimeCollection.Entities.First();

                //Create email attachment

                Entity _EmailAttachment = new Entity("activitymimeattachment");

                if (_NotesAttachment.Contains("subject"))

                    _EmailAttachment["subject"] = _NotesAttachment.GetAttributeValue<string>("subject");

 

                _EmailAttachment["objectid"] = new EntityReference("email", _ResultEntity.Id);

                _EmailAttachment["objecttypecode"] = "email";

                if (_NotesAttachment.Contains("filename"))

                    _EmailAttachment["filename"] = _NotesAttachment.GetAttributeValue<string>("filename");

                if (_NotesAttachment.Contains("documentbody"))

                    _EmailAttachment["body"] = _NotesAttachment.GetAttributeValue<string>("documentbody");

                if (_NotesAttachment.Contains("mimetype"))

                    _EmailAttachment["mimetype"] = _NotesAttachment.GetAttributeValue<string>("mimetype");

 

                service.Create(_EmailAttachment);

            }

            // Sending email

            SendEmailRequest reqSendEmail = new SendEmailRequest();

            reqSendEmail.EmailId = _ResultEntity.Id;

            reqSendEmail.TrackingToken = "";

            reqSendEmail.IssueSend = true;

            SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);

        }

    }

}

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 "Account" in the entity drop down and workflow in the category. Now use the following steps to configure the workflow.

  • Add a condition to check account approval
  • Add a step to create an email record that we can use in a custom step
  • Add our custom workflow steps from "Add Step" as in the following:

custom workflow 

Click on the "Set Properties" button and set the input parameter from the Form Assistant as in the following:

Set Properties

It should look as in the following after completion:

Send mail

Save and Activate your workflow and test.

Up Next
    Ebook Download
    View all
    Learn
    View all