If you are looking for sending email from entity record attachment from then this post is going to help you to implement this requirement. We are going to explain here how we can send email using workflow with attachment from notes in regarding entity. Let’s consider a business scenario where we have implemented Customer approval process and as soon as customer is approved we need to send him notification with attachment in notes, this attachment contains details about his account.
We can’t access attachment from notes in OOB workflow so we need to write a custom workflow where we need to get entity record attachment from annotation entity and need to create activitymimeattachment record and need to associate it with email created. While creating workflow we need to use create step instead of send email so that we can refer email record id.
So mainly we need to below steps to implement our requirement:
- Develop custom workflow assembly to get attachment.
- Create workflow and use custom workflow assembly step to send email.
Let’s create custom workflow, if you are new to custom workflow assembly then please refer our previous post to create custom workflow. Create a class library project and add required assemblies to your project. let’s rename our class file as SendEmailWithAttachement and use below 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.
Click on Set Properties button to set input variable like below
After completion it should look like below
Save and Activate workflow.

Like
Report

*This post is locked for comments