{"id":1672,"date":"2015-10-03T12:40:44","date_gmt":"2015-10-03T12:40:44","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=1672"},"modified":"2015-11-30T10:33:06","modified_gmt":"2015-11-30T10:33:06","slug":"sending-history-notes-using-custom-workflow","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=1672","title":{"rendered":"Sending History Notes using Custom Workflow"},"content":{"rendered":"<p>Long time back we wrote a <a href=\"https:\/\/himbap.com\/blog\/?p=431\">post<\/a> to get history notes using custom workflow, in this post we are providing updated version of the\u00a0code supported for CRM 2011\/2013\/2015.<\/p>\n<p><strong>Requirement:<\/strong> If you have requirement to send notification when new note is added to any entity record where you have notes relationship enabled, for example\u00a0let&#8217;s say when any notes entered to any\u00a0critical opportunity,\u00a0you want to notify\u00a0to\u00a0sales manager and want to send him latest notes with history notes as well.<\/p>\n<p><strong>Solution:<\/strong> We can write custom workflow for this, you can use source code from this post to build custom workflow and can register it to CRM environment. If you are new to Custom workflow development then first check our <a href=\"https:\/\/himbap.com\/blog\/?p=780\">Step By Step Creating Custom Workflow,<\/a> post\u00a0for how to setup custom workflow assembly using Visual Studio.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">public class GetHistoricalNotes: CodeActivity {\r\n\t[Output(&quot;HistoryNotes&quot;)]\r\n\tpublic InOutArgument &lt;string&gt; HistoryNotes {\r\n\t\tget;\r\n\t\tset;\r\n\t}\r\n\tprotected override void Execute(CodeActivityContext context) {\r\n\t\tstring notestMessage = string.Empty;\r\n\t\t\/\/Create the tracing service\r\n\t\tITracingService tracingService = context.GetExtension &lt; ITracingService &gt; ();\r\n\t\t\/\/Create the context\r\n\t\tIWorkflowContext wkfContext = context.GetExtension &lt; IWorkflowContext &gt; ();\r\n\t\tIOrganizationServiceFactory serviceFactory = context.GetExtension &lt; IOrganizationServiceFactory &gt; ();\r\n\t\tIOrganizationService service = serviceFactory.CreateOrganizationService(wkfContext.UserId);\r\n\t\t\/\/Get RegardingID\r\n\t\tEntity currentNotes = (Entity) service.Retrieve(&quot;annotation&quot;, wkfContext.PrimaryEntityId, new ColumnSet(new string[] {\r\n\t\t\t&quot;objectid&quot;;\r\n\t\t}));\r\n\t\t\/\/GetAll Related Notes\r\n\t\tnotestMessage = GetNotes(currentNotes.GetAttributeValue &lt; EntityReference &gt; &quot;objectid&quot;).Id, service);\r\n\tthis.HistoryNotes.Set(context, notestMessage);\r\n}<\/pre>\n<p>In above code we are reading additional columns from annotation (notes) entity and passing regardingobject id to below method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">private string GetNotes(Guid regarding, IOrganizationService crmservice) {\r\n\tStringBuilder notesStringBuilder = new StringBuilder();\r\n\tColumnSet columSet = new ColumnSet(new string[] {\r\n\t\t&quot;notetext&quot;, &quot;objectid&quot;, &quot;subject&quot;, &quot;createdby&quot;, &quot;createdon&quot;\r\n\t});\r\n\tQueryExpression query = new QueryExpression() {\r\n\t\tEntityName = &quot;annotation&quot;,\r\n\t\tColumnSet = columSet,\r\n\t\tCriteria = {\r\n\t\t\tConditions = {\r\n\t\t\t\tnew ConditionExpression(&quot;objectid&quot;, ConditionOperator.Equal, regarding)\r\n\t\t\t},\r\n\t\t},\r\n\t};\r\n\tEntityCollection historynotesCollection = crmservice.RetrieveMultiple(query);\r\n\tif (historynotesCollection != null &amp;&amp; historynotesCollection.Entities.Count &gt; 0) {\r\n\t\tforeach(Entity notes in historynotesCollection.Entities) {\r\n\t\t\tif (notes.Contains(&quot;subject&quot;) &amp;&amp; !string.IsNullOrEmpty(notes[&quot;subject&quot;].ToString())) {\r\n\t\t\t\tnotesStringBuilder.AppendLine(&quot;Subject :&quot; + notes[&quot;subject&quot;].ToString());\r\n\t\t\t}\r\n\t\t\tif (notes.Contains(&quot;notetext&quot;) &amp;&amp; !string.IsNullOrEmpty(notes[&quot;notetext&quot;].ToString())) {\r\n\t\t\t\tnotesStringBuilder.AppendLine(&quot;\\n Description :&quot; + notes[&quot;notetext&quot;].ToString());\r\n\t\t\t}\r\n\t\t\tif (notes.Contains(&quot;createdon&quot;) &amp;&amp; !string.IsNullOrEmpty(notes[&quot;createdon&quot;].ToString())) {\r\n\t\t\t\tnotesStringBuilder.AppendLine(&quot;\\n CreatedOn :&quot; + notes.GetAttributeValue &lt; DateTime &gt; (&quot;createdon&quot;).ToShortDateString());\r\n\t\t\t}\r\n\t\t\tif (notes.Contains(&quot;createdby&quot;) &amp;&amp; !string.IsNullOrEmpty(notes[&quot;createdby&quot;].ToString())) {\r\n\t\t\t\tnotesStringBuilder.AppendLine(&quot;\\n Created By :&quot; + notes.GetAttributeValue &lt; EntityReference &gt; (&quot;createdby&quot;).Name);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn notesStringBuilder.ToString();\r\n}\r\n<\/pre>\n<p>After using above code we can build our workflow assembly and can register to our CRM environment using latest plug-in registration tool. After that we can design a workflow where first we need to select our custom workflow step and then we can use it in email step like below:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/10\/HistoryNotes.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-1674 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/10\/HistoryNotes-300x110.png\" alt=\"HistoryNotes\" width=\"300\" height=\"110\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/10\/HistoryNotes-300x110.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/10\/HistoryNotes-624x229.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2015\/10\/HistoryNotes.png 998w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Long time back we wrote a post to get history notes using custom workflow, in this post we are providing updated version of the\u00a0code supported for CRM 2011\/2013\/2015. Requirement: If you have requirement to send notification when new note is added to any entity record where you have notes relationship enabled, for example\u00a0let&#8217;s say when any notes entered to any\u00a0critical&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=1672\">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,6,8],"tags":[219,220,263],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1672"}],"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=1672"}],"version-history":[{"count":10,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1672\/revisions"}],"predecessor-version":[{"id":1893,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1672\/revisions\/1893"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}