Sending Email with entity attachment from 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

{

//define output variable

[Input(“SourceEmail”)]

[ReferenceTarget(“email”)]

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

protected override void Execute(CodeActivityContext executionContext)

{

// Get workflow context

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

//Create service factory

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

// Create Organization service

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

AddAttachmentToEmailRecord(service, context.PrimaryEntityId, SourceEmail.Get<EntityReference>(executionContext));

}

private void AddAttachmentToEmailRecord(IOrganizationService service, Guid SourceAccountID, EntityReference SourceEmailID)

{

//create email object

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

EntityCollection _MimeCollection = service.RetrieveMultiple(_QueryNotes);

if (_MimeCollection.Entities.Count > 0)

{  //we need to fetch first attachment

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 SendEmail = new SendEmailRequest();

SendEmail.EmailId = _ResultEntity.Id;

SendEmail.TrackingToken = “”;

SendEmail.IssueSend = true;

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

}

}

}

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

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

. Now follow below steps to configure workflow

  •  Add condition to check account approval.
  • Add a step to create email record that we can use in custom step
  • Add our custom workflow steps from Add Step.

2014-01-20_133542

Click on Set Properties button to set input variable like below

inputvariableAfter completion it should look like below

emailCompete

Save and Activate workflow.

9 thoughts on “Sending Email with entity attachment from Notes

  1. Pingback: TechNet Blogs

  2. Pingback: CRM Best Practices | Somesh Blogs

  3. RAJNIKANT

    Error 1 The name ‘SourceTrialRequestID’ does not exist in the current context E:Rajnikant’s DataVSSendEmailWithAttachementSendEmailWithAttachementSendEmailWithAttachements.cs 50 108 SendEmailWithAttachement

    Reply
  4. SRS

    Hi Mahendrapal, is it possible to test this using a console app, also if possible could you please guide further

    Reply
    1. HIMBAP Post author

      Yes, you can do that, you could test this in console application and pass required parameter.

      Reply
      1. Ignacio

        well, I’ve done it but when I try to set the input variable it doesn’t appear 🙁
        here’s mi code

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Activities;
        using Microsoft.Xrm.Sdk;
        using Microsoft.Xrm.Sdk.Workflow;
        using Microsoft.Xrm.Sdk.Query;
        using Microsoft.Xrm.Sdk.Messages;
        using Microsoft.Xrm.Sdk.Client;
        using Microsoft.Xrm.Sdk.Discovery;
        using Microsoft.Crm.Sdk.Messages;

        namespace EmailAttachment
        {
        public class SendEmailWithAttachement : CodeActivity
        {

        //define output variable
        [Input(“SourceEmail”)]
        [ReferenceTarget(“email”)]
        public InArgument SourceEmail { get; set; }

        protected override void Execute(CodeActivityContext executionContext)
        {
        // Get workflow context
        IWorkflowContext context = executionContext.GetExtension();
        //Create service factory
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension();
        // Create Organization service
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
        // Get the target entity from the context
        Entity OpportunitiesID = (Entity)service.Retrieve(“opportunity”, context.PrimaryEntityId, new ColumnSet(new string[] { “opportunityid” }));

        AddAttachmentToEmailRecord(service, OpportunitiesID.Id, SourceEmail.Get(executionContext));
        }

        private void AddAttachmentToEmailRecord(IOrganizationService service, Guid OpportunitiesID, EntityReference SourceEmailID)
        {
        //create email object
        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, OpportunitiesID));

        EntityCollection _MimeCollection = service.RetrieveMultiple(_QueryNotes);

        if (_MimeCollection.Entities.Count > 0){
        //we need to fetch first attachment
        Entity _NotesAttachment = _MimeCollection.Entities.First();
        //Create email attachment
        Entity _EmailAttachment = new Entity(“activitymimeattachment”);
        if (_NotesAttachment.Contains(“subject”))
        _EmailAttachment[“subject”] = _NotesAttachment.GetAttributeValue(“subject”);
        _EmailAttachment[“objectid”] = new EntityReference(“email”, _ResultEntity.Id);
        _EmailAttachment[“objecttypecode”] = “email”;
        if (_NotesAttachment.Contains(“filename”))
        _EmailAttachment[“filename”] = _NotesAttachment.GetAttributeValue(“filename”);
        if (_NotesAttachment.Contains(“documentbody”))
        _EmailAttachment[“body”] = _NotesAttachment.GetAttributeValue(“documentbody”);
        if (_NotesAttachment.Contains(“mimetype”))
        _EmailAttachment[“mimetype”] = _NotesAttachment.GetAttributeValue(“mimetype”);
        service.Create(_EmailAttachment);
        }

        SendEmailRequest SendEmail = new SendEmailRequest();
        SendEmail.EmailId = _ResultEntity.Id;
        SendEmail.TrackingToken = “”;
        SendEmail.IssueSend = true;
        SendEmailResponse res = (SendEmailResponse)service.Execute(SendEmail);
        }

        }

        }

        Reply

Leave a Reply

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