{"id":431,"date":"2011-03-18T17:49:51","date_gmt":"2011-03-18T17:49:51","guid":{"rendered":"http:\/\/mahenderpal.wordpress.com\/?p=431"},"modified":"2015-10-03T05:15:25","modified_gmt":"2015-10-03T05:15:25","slug":"send-all-notes-in-email-through-workflow-with-record-link","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=431","title":{"rendered":"Send All notes in Email through Workflow with Record link"},"content":{"rendered":"<p>As we all know in CRM notes are stored in annotation entity, like records. For example if you have added 3 notes in account there will be three different record in annotation entity related to that account. In my current project we got requirement to send Notification when a new notes is added to Opportunity. This can be easily achieved by OOB workflow. But while send notes we also have to include all previous notes for that opportunity, this can\u2019t achieved through OOB workflow directly. Also we need to send link of that opportunity in notification which is again can\u2019t be achieved through OOB workflow. So I decided to write a custom workflow to implement both these functionality below are the detail steps and code.<br \/>\nStep1. Create Sequence workflow library project from Visual Studio 2008.<br \/>\nStep2. Give appropriate name to your custom workflow, I am using AttachedNotes.<br \/>\nStep3. Right click on Design and select view code.<br \/>\nStep4. You can use below code<br \/>\nusing System;<br \/>\nusing System.ComponentModel;<br \/>\nusing System.ComponentModel.Design;<br \/>\nusing System.Collections;<br \/>\nusing System.Drawing;<br \/>\nusing System.Linq;<br \/>\nusing System.Workflow.ComponentModel.Compiler;<br \/>\nusing System.Workflow.ComponentModel.Serialization;<br \/>\nusing System.Workflow.ComponentModel;<br \/>\nusing System.Workflow.ComponentModel.Design;<br \/>\nusing System.Workflow.Runtime;<br \/>\nusing System.Workflow.Activities;<br \/>\nusing System.Workflow.Activities.Rules;<br \/>\nusing Microsoft.Crm.Sdk;<br \/>\nusing Microsoft.Crm.SdkTypeProxy;<br \/>\nusing Microsoft.Crm.Sdk.Query;<br \/>\nusing Microsoft.Crm.Workflow;<br \/>\nusing System.IO;<br \/>\nusing System.Text;<br \/>\nnamespace AttachNotes<br \/>\n{<br \/>\n[CrmWorkflowActivity(&#8220;IncludeHistorialNotes&#8221;, &#8220;HistorialNotesUtility&#8221;)]<br \/>\npublic sealed partial class NoteWF: SequentialWorkflowActivity<br \/>\n{<br \/>\nGuid _OpportunityGUID = Guid.Empty;<br \/>\npublic NoteWF()<br \/>\n{<br \/>\nInitializeComponent();<br \/>\n}<br \/>\npublic static DependencyProperty HistoryNotesProperty = DependencyProperty.Register(&#8220;HistoryNotes&#8221;, typeof(string), typeof(NoteWF));<br \/>\n[CrmOutput(&#8220;HistoryNote&#8221;)]<br \/>\npublic string HistoryNotes<br \/>\n{<br \/>\nget { return (string)GetValue(HistoryNotesProperty); }<\/p>\n<p>set<br \/>\n{<br \/>\nSetValue(HistoryNotesProperty, value);<br \/>\n}<br \/>\n}<\/p>\n<p>public static DependencyProperty UrlProperty = DependencyProperty.Register(&#8220;Url&#8221;, typeof(string), typeof(NoteWF));<br \/>\n[CrmInput(&#8220;Url&#8221;)]<br \/>\npublic string Url<br \/>\n{<br \/>\nget { return (string)base.GetValue(UrlProperty); }<br \/>\nset { base.SetValue(UrlProperty, value); }<br \/>\n}<\/p>\n<p>public static DependencyProperty ReturnUrlProperty = DependencyProperty.Register(&#8220;ReturnUrl&#8221;, typeof(string), typeof(NoteWF));<br \/>\n[CrmOutput(&#8220;ReturnUrl&#8221;)]<br \/>\npublic string ReturnUrl<br \/>\n{<br \/>\nget { return (string)base.GetValue(ReturnUrlProperty); }<br \/>\nset { base.SetValue(ReturnUrlProperty, value); }<br \/>\n}<\/p>\n<p>protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)<br \/>\n{<br \/>\nstring _NotestMessage = string.Empty;<br \/>\nIContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));<br \/>\nIWorkflowContext context = contextService.Context;<br \/>\nICrmService crmService = context.CreateCrmService(true);<\/p>\n<p>\/\/Get RegardingID<br \/>\nannotation _CurrentNotes = (annotation)crmService.Retrieve(EntityName.annotation.ToString(), context.PrimaryEntityId, new ColumnSet(new string[] { &#8220;objectid&#8221; }));<br \/>\n\/\/GetAll Related Notes<br \/>\n_NotestMessage = GetNotes(_CurrentNotes.objectid.Value, crmService);<\/p>\n<p>this.HistoryNotes = _NotestMessage;<br \/>\nstring fullUrl = string.Concat(this.Url, _CurrentNotes.objectid.Value.ToString());<br \/>\nthis.ReturnUrl = string.Format(@&#8221;<a>{1}<\/a>&#8220;, fullUrl, &#8220;Click To Open Opportnity&#8221;);<\/p>\n<p>return ActivityExecutionStatus.Closed;<br \/>\n}<\/p>\n<p>private string GetNotes(Guid Regarding, ICrmService _iService)<br \/>\n{<\/p>\n<p>StringBuilder _NotesMessage = new StringBuilder();<\/p>\n<p>ColumnSet _ColsToFetch = new ColumnSet(new string[] { &#8220;notetext&#8221;, &#8220;objectid&#8221;, &#8220;subject&#8221;,&#8221;createdby&#8221;, &#8220;createdon&#8221; });<\/p>\n<p>RetrieveMultipleResponse retrieved = null;<br \/>\nConditionExpression _Condition = new ConditionExpression();<br \/>\n_Condition.Operator = ConditionOperator.Equal;<br \/>\n_Condition.AttributeName = &#8220;objectid&#8221;;<br \/>\n_Condition.Values = new object[] { Regarding };<\/p>\n<p>FilterExpression _Filter = new FilterExpression();<br \/>\n_Filter.FilterOperator = LogicalOperator.And;<br \/>\n_Filter.Conditions.AddRange(new ConditionExpression[] { _Condition });<\/p>\n<p>QueryExpression query = new QueryExpression();<\/p>\n<p>query.ColumnSet = _ColsToFetch;<br \/>\nquery.Criteria = _Filter;<br \/>\nquery.EntityName = EntityName.annotation.ToString();<br \/>\nRetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();<br \/>\nretrieve.Query = query;<br \/>\nretrieve.ReturnDynamicEntities = true;<br \/>\nretrieved = (RetrieveMultipleResponse)_iService.Execute(retrieve);<\/p>\n<p>if (retrieved.BusinessEntityCollection.BusinessEntities.Count &gt; 0)<br \/>\n{<br \/>\nfor (int i = 0; i &lt; retrieved.BusinessEntityCollection.BusinessEntities.Count; i++)<br \/>\n{\/\/collect all notes regarding the same opportunity<br \/>\nDynamicEntity _Notes = (DynamicEntity)retrieved.BusinessEntityCollection.BusinessEntities[i];<br \/>\nif (_Notes.Properties.Contains(&#8220;subject&#8221;))<br \/>\n{<br \/>\n_NotesMessage.AppendLine(_Notes.Properties[&#8220;subject&#8221;].ToString());<br \/>\n}<\/p>\n<p>if (_Notes.Properties.Contains(&#8220;notetext&#8221;))<br \/>\n{<br \/>\n_NotesMessage.AppendLine(_Notes.Properties[&#8220;notetext&#8221;].ToString());<\/p>\n<p>}<\/p>\n<p>}<br \/>\n}<br \/>\nreturn _NotesMessage.ToString();<\/p>\n<p>}<br \/>\n}<\/p>\n<p>}<br \/>\nStep5. Sign your custom workflow assembly.<br \/>\nStep6. Register your workflow assembly.<br \/>\nStep7. Create workflow, select Note entity and select to run when record is created.<br \/>\nYou will get your custom workflow section like below<br \/>\n<a href=\"http:\/\/mahenderpal.files.wordpress.com\/2011\/03\/customwfstep.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-432\" title=\"customWFstep\" src=\"http:\/\/mahenderpal.files.wordpress.com\/2011\/03\/customwfstep.jpg?w=285\" alt=\"\" width=\"285\" height=\"300\" \/><\/a><\/p>\n<p>Step8. Select your custom workflow step<br \/>\nStep9. Click on Set Properties and fill URL for opportunity<br \/>\n<a href=\"http:\/\/mahenderpal.files.wordpress.com\/2011\/03\/cutomwfrul.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-436\" title=\"cutomWFRUL\" src=\"http:\/\/mahenderpal.files.wordpress.com\/2011\/03\/cutomwfrul.png?w=300\" alt=\"\" width=\"300\" height=\"31\" \/><\/a><br \/>\nStep10.Select Send Email step and click on set Properties and set properties accordingly.<br \/>\nStep11. Select your custom workflow from look for picklist and select both output variable to provide url link and History notes.<br \/>\n<a href=\"http:\/\/mahenderpal.files.wordpress.com\/2011\/03\/customnlink.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-435\" title=\"CustomnLink\" src=\"http:\/\/mahenderpal.files.wordpress.com\/2011\/03\/customnlink.jpg?w=300\" alt=\"\" width=\"300\" height=\"40\" \/><\/a><br \/>\nStep12. Save and Publish workflow.<br \/>\nNext time when you will add note to opportunity you will get email with history notes and URL for that opportunity record.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we all know in CRM notes are stored in annotation entity, like records. For example if you have added 3 notes in account there will be three different record in annotation entity related to that account. In my current project we got requirement to send Notification when a new notes is added to Opportunity. This can be easily achieved&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=431\">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":[22,10,1,20],"tags":[],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/431"}],"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=431"}],"version-history":[{"count":1,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":1671,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions\/1671"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}