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

// Get the target entity from the context

Entity  AccountID= (Entity)service.Retrieve(“account”, context.PrimaryEntityId, new ColumnSet(new string[] { “accountid” }));

AddAttachmentToEmailRecord(service, AccountID.Id, 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, SourceTrialRequestID));

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.

About these ads

1 Comment

Filed under MS CRM 2011

One response to “Sending Email with entity attachment from Notes

  1. Great example! Thanks so much for sharing.

    Mike

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s