{"id":1235,"date":"2014-01-20T08:20:21","date_gmt":"2014-01-20T08:20:21","guid":{"rendered":"http:\/\/mahenderpal.wordpress.com\/?p=1235"},"modified":"2021-06-19T08:28:15","modified_gmt":"2021-06-19T08:28:15","slug":"sending-email-with-entity-attachment-from-notes","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=1235","title":{"rendered":"Sending Email with entity attachment from Notes"},"content":{"rendered":"<p>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\u2019s 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.<\/p>\n<p>We can\u2019t 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.<\/p>\n<p>So mainly we need to use the following procedure to implement our requirements:<\/p>\n<ul>\n<li>Develop a custom workflow assembly to get an attachment.<\/li>\n<li>Create a workflow and use a custom workflow assembly step to send the email.<\/li>\n<\/ul>\n<p>Let\u2019s create a custom workflow. If you are new to custom workflow assemblies then please refer to our previous article to<a href=\"http:\/\/mahenderpal.wordpress.com\/2012\/08\/26\/step-by-step-creating-custom-workflow-in-microsoft-crm-2011\/\"> create a custom workflow<\/a>. Create a class library project and add the required assemblies to your project. Let\u2019s rename our class file to SendEmailWithAttachement and use the following code:<\/p>\n<p>namespace EmailAttachment<\/p>\n<p>{<\/p>\n<p>public class SendEmailWithAttachement : CodeActivity<\/p>\n<p>{<\/p>\n<p>\/\/define output variable<\/p>\n<p>[Input(&#8220;SourceEmail&#8221;)]<\/p>\n<p>[ReferenceTarget(&#8220;email&#8221;)]<\/p>\n<p>public InArgument&lt;EntityReference&gt; SourceEmail { get; set; }<\/p>\n<p>protected override void Execute(CodeActivityContext executionContext)<\/p>\n<p>{<\/p>\n<p>\/\/ Get workflow context<\/p>\n<p>IWorkflowContext context = executionContext.GetExtension&lt;IWorkflowContext&gt;();<\/p>\n<p>\/\/Create service factory<\/p>\n<p>IOrganizationServiceFactory serviceFactory =\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 executionContext.GetExtension&lt;IOrganizationServiceFactory&gt;();<\/p>\n<p>\/\/ Create Organization service<\/p>\n<p>IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);<\/p>\n<p>AddAttachmentToEmailRecord(service, context.PrimaryEntityId, SourceEmail.Get&lt;EntityReference&gt;(executionContext));<\/p>\n<p>}<\/p>\n<p>private void AddAttachmentToEmailRecord(IOrganizationService service, Guid SourceAccountID, EntityReference SourceEmailID)<\/p>\n<p>{<\/p>\n<p>\/\/create email object<\/p>\n<p>Entity _ResultEntity = service.Retrieve(&#8220;email&#8221;, SourceEmailID.Id, new ColumnSet(true));<\/p>\n<p>QueryExpression _QueryNotes = new QueryExpression(&#8220;annotation&#8221;);<\/p>\n<p>_QueryNotes.ColumnSet = new ColumnSet(new string[] { &#8220;subject&#8221;, &#8220;mimetype&#8221;, &#8220;filename&#8221;, &#8220;documentbody&#8221; });<\/p>\n<p>_QueryNotes.Criteria = new FilterExpression();<\/p>\n<p>_QueryNotes.Criteria.FilterOperator = LogicalOperator.And;<\/p>\n<p>_QueryNotes.Criteria.AddCondition(new ConditionExpression(&#8220;objectid&#8221;, ConditionOperator.Equal, SourceAccountID));<\/p>\n<p>EntityCollection _MimeCollection = service.RetrieveMultiple(_QueryNotes);<\/p>\n<p>if (_MimeCollection.Entities.Count &gt; 0)<\/p>\n<p>{\u00a0 \/\/we need to fetch first attachment<\/p>\n<p>Entity _NotesAttachment = _MimeCollection.Entities.First();<\/p>\n<p>\/\/Create email attachment<\/p>\n<p>Entity _EmailAttachment = new Entity(&#8220;activitymimeattachment&#8221;);<\/p>\n<p>if (_NotesAttachment.Contains(&#8220;subject&#8221;))<\/p>\n<p>_EmailAttachment[&#8220;subject&#8221;] = _NotesAttachment.GetAttributeValue&lt;string&gt;(&#8220;subject&#8221;);<\/p>\n<p>_EmailAttachment[&#8220;objectid&#8221;] = new EntityReference(&#8220;email&#8221;, _ResultEntity.Id);<\/p>\n<p>_EmailAttachment[&#8220;objecttypecode&#8221;] = &#8220;email&#8221;;<\/p>\n<p>if (_NotesAttachment.Contains(&#8220;filename&#8221;))<\/p>\n<p>_EmailAttachment[&#8220;filename&#8221;] = _NotesAttachment.GetAttributeValue&lt;string&gt;(&#8220;filename&#8221;);<\/p>\n<p>if (_NotesAttachment.Contains(&#8220;documentbody&#8221;))<\/p>\n<p>_EmailAttachment[&#8220;body&#8221;] = _NotesAttachment.GetAttributeValue&lt;string&gt;(&#8220;documentbody&#8221;);<\/p>\n<p>if (_NotesAttachment.Contains(&#8220;mimetype&#8221;))<\/p>\n<p>_EmailAttachment[&#8220;mimetype&#8221;] = _NotesAttachment.GetAttributeValue&lt;string&gt;(&#8220;mimetype&#8221;);<\/p>\n<p>service.Create(_EmailAttachment);<\/p>\n<p>}<\/p>\n<p>\/\/ Sending email<\/p>\n<p>SendEmailRequest SendEmail = new SendEmailRequest();<\/p>\n<p>SendEmail.EmailId = _ResultEntity.Id;<\/p>\n<p>SendEmail.TrackingToken = &#8220;&#8221;;<\/p>\n<p>SendEmail.IssueSend = true;<\/p>\n<p>SendEmailResponse res = (SendEmailResponse)service.Execute(SendEmail);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Build and register your assembly using plug-in registration tool. Now we need to create our workflow to use our custom assembly step.<\/p>\n<p>Navigate to Settings-&gt;Process to create new process. Select Account in entity drop down and workflow in category.<\/p>\n<p>. Now follow below steps to configure workflow<\/p>\n<ul>\n<li>\u00a0Add condition to check account approval.<\/li>\n<li>Add a step to create email record that we can use in custom step<\/li>\n<li>Add our custom workflow steps from Add Step.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/mahenderpal.files.wordpress.com\/2014\/01\/2014-01-20_133542.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1236 aligncenter\" src=\"http:\/\/mahenderpal.files.wordpress.com\/2014\/01\/2014-01-20_133542.png\" alt=\"2014-01-20_133542\" width=\"500\" height=\"240\" \/><\/a><\/p>\n<p>Click on Set Properties button to set input variable like below<\/p>\n<p><a href=\"http:\/\/mahenderpal.files.wordpress.com\/2014\/01\/inputvariable.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1237 aligncenter\" src=\"http:\/\/mahenderpal.files.wordpress.com\/2014\/01\/inputvariable.png\" alt=\"inputvariable\" width=\"500\" height=\"151\" \/><\/a>After completion it should look like below<\/p>\n<p><a href=\"http:\/\/mahenderpal.files.wordpress.com\/2014\/01\/emailcompete.png\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1238 aligncenter\" src=\"http:\/\/mahenderpal.files.wordpress.com\/2014\/01\/emailcompete.png\" alt=\"emailCompete\" width=\"500\" height=\"161\" \/><\/a><\/p>\n<p>Save and Activate workflow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s consider a business scenario where we have implemented a customer approval process and as&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=1235\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[34,167,168,188],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1235"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1235"}],"version-history":[{"count":2,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1235\/revisions"}],"predecessor-version":[{"id":4184,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1235\/revisions\/4184"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}