{"id":33407,"date":"2022-12-20T15:47:40","date_gmt":"2022-12-20T10:17:40","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=33407"},"modified":"2023-07-17T11:51:49","modified_gmt":"2023-07-17T06:21:49","slug":"how-to-trigger-the-plugin-on-any-business-process-flow-stage-change-and-update-custom-entity-record-related-to-the-active-stage","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2022\/12\/how-to-trigger-the-plugin-on-any-business-process-flow-stage-change-and-update-custom-entity-record-related-to-the-active-stage\/","title":{"rendered":"How to trigger the plugin on any Business Process Flow stage change and update custom entity record related to the Active Stage"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>Recently while working on a customer requirement, we came across a scenario where we needed to trigger a plugin on change of Business Process Flow stages. So, in the pursuit of finding a solution, we came across workflow and process stage entities, but they were not very useful in this scenario. Our requirement was to trigger the plugin on the <a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/business-process-dynamics-365-crm-to-do-checklist-sequence\/\" target=\"_blank\" rel=\"noopener\">Business Process Flow<\/a> stage change of an Employee entity. So, we tried writing a plugin for the update of an Employee entity. There\u2019s one field \u201cstageid\u201d on every entity on which a Business process flow is configured. We thought on the change of the Business Process Flow stage this field might be getting updated with the Id of the current active stage, which we can further use to fulfill our requirement. But to our disappointment, this field is now deprecated and thus we are left empty-handed.<\/p>\n<p>Further investigation brought light to the reality i.e., for every Business Process Flow, a subsequent Entity gets created in Dynamic365 CRM. In this blog, we shall further see how using this entity, we achieved the customer\u2019s requirement.<\/p>\n<h2><strong>Scenario:<\/strong><\/h2>\n<p>To handle the recruitment process there\u2019s an Employee table as well as an Employee Stages table. We want to update the status of the related Employee Stage record for whichever stage currently is active.<\/p>\n<p>In the below Image, we can see that even when we changed the stage of the Employee record the stage id stays blank. In the name itself, we can see it is stated that it\u2019s now deprecated.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-33413\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/1Business-Process-Flow-stage.jpeg\" alt=\"Business Process Flow stage\" width=\"845\" height=\"355\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/1Business-Process-Flow-stage.jpeg 759w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/1Business-Process-Flow-stage-300x126.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/1Business-Process-Flow-stage-660x277.jpeg 660w\" sizes=\"(max-width: 845px) 100vw, 845px\" \/><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>The below image represents the Business Process Flow on the Employee Entity to handle the employee recruitment process.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-33412\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/2Business-Process-Flow-stage.jpeg\" alt=\"Business Process Flow stage\" width=\"1339\" height=\"499\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/2Business-Process-Flow-stage.jpeg 1339w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/2Business-Process-Flow-stage-300x112.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/2Business-Process-Flow-stage-1024x382.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/2Business-Process-Flow-stage-768x286.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/2Business-Process-Flow-stage-660x246.jpeg 660w\" sizes=\"(max-width: 1339px) 100vw, 1339px\" \/><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-33411\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/3Business-Process-Flow-stage.jpeg\" alt=\"Business Process Flow stage\" width=\"1361\" height=\"496\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/3Business-Process-Flow-stage.jpeg 1361w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/3Business-Process-Flow-stage-300x109.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/3Business-Process-Flow-stage-1024x373.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/3Business-Process-Flow-stage-768x280.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/3Business-Process-Flow-stage-660x241.jpeg 660w\" sizes=\"(max-width: 1361px) 100vw, 1361px\" \/><\/p>\n<p>In the above screenshot, we can see that CRM created an entity for the custom Business Process Flow, and it contains the \u201c<strong>activestageid<\/strong>\u201d field which has the current active stage for a particular record.<\/p>\n<p>Now we will write a plugin on the update of the above<strong> Business Process Flow entity <\/strong>and add the filtering attribute of \u201c<strong>activestageid<\/strong>\u201d.<\/p>\n<p><strong><img decoding=\"async\" class=\"alignnone size-full wp-image-33410\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/4Business-Process-Flow-stage.jpeg\" alt=\"Business Process Flow stage\" width=\"907\" height=\"475\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/4Business-Process-Flow-stage.jpeg 907w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/4Business-Process-Flow-stage-300x157.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/4Business-Process-Flow-stage-768x402.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/4Business-Process-Flow-stage-660x346.jpeg 660w\" sizes=\"(max-width: 907px) 100vw, 907px\" \/><\/strong>In the below image, we find the first record of the Employee Stages entity having the status as Active, and the remaining records are currently in an Inactive state.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-33409\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/5Business-Process-Flow-stage.jpeg\" alt=\"Business Process Flow stage\" width=\"1261\" height=\"553\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/5Business-Process-Flow-stage.jpeg 1261w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/5Business-Process-Flow-stage-300x132.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/5Business-Process-Flow-stage-1024x449.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/5Business-Process-Flow-stage-768x337.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/5Business-Process-Flow-stage-660x289.jpeg 660w\" sizes=\"(max-width: 1261px) 100vw, 1261px\" \/><\/p>\n<p>When we change the stage of the Business Process Flow from Sourcing to Screening, using our plugin we are changing the status of the related Employee Stages entity record named Screening to Active.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-33408\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/6Business-Process-Flow-stage.jpeg\" alt=\"Business Process Flow stage\" width=\"1266\" height=\"566\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/6Business-Process-Flow-stage.jpeg 1266w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/6Business-Process-Flow-stage-300x134.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/6Business-Process-Flow-stage-1024x458.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/6Business-Process-Flow-stage-768x343.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/6Business-Process-Flow-stage-660x295.jpeg 660w\" sizes=\"(max-width: 1266px) 100vw, 1266px\" \/><\/p>\n<p><strong>Code: <\/strong><\/p>\n<pre class=\"lang:css gutter:true start:1\">private void changeBPFstage(PluginConfig config, StringBuilder traceLogBuilder, Entity targetEntity)\r\n\r\n{\r\n\r\n\/\/Get current BPF activestageID\r\n\r\nEntityReference currentBpfStageId = targetEntity.Attributes.Contains(\"activestageid\") ? targetEntity.GetAttributeValue&lt;EntityReference&gt;(\"activestageid\") : null;\r\n\r\n\/\/Execute Fetchxml\r\n\r\nstring fetchXML = \"&lt; fetch &gt;\" +\r\n\r\n\" &lt;entity name='new_employeestage'&gt;\" +\r\n\r\n\" &lt;attribute name='new_ employeestageid'\/&gt;\" +\r\n\r\n\" &lt;filter type ='and'&gt;\" +\r\n\r\n\" &lt;condition attribute='new_ employeestageid' operator= 'eq' value='\"+currentBpfStageId+\"'\/&gt;\" +\r\n\r\n\" &lt;\/ filter&gt;\" +\r\n\r\n\"&lt;\/ entity&gt;\" +\r\n\r\n\" &lt;\/fetch&gt;\";\r\n\r\n\u00a0\r\n\r\nEntityCollection entityCollection = config.Service.RetrieveMultiple(new FetchExpression(fetchXML));\r\n\r\n\/\/To check entitycollection count is greater than 0\r\n\r\nif(entityCollection.Entities.Count&gt;0)\r\n\r\n{\r\n\r\n\/\/Update status of employee record\r\n\r\nEntity entemployee = new Entity(\"new_employeestage\", targetEntity.Id);\r\n\r\nentemployee.Attributes[\"statuscode\"] = new OptionSetValue(1);\r\n\r\nentemployee.Attributes[\"statecode\"] = new OptionSetValue(0);\r\n\r\nconfig.Service.Update(entemployee);\r\n\r\n}\r\n\r\n}<\/pre>\n<h2>Conclusion<\/h2>\n<p>In this way, we can easily call the plugin on the Business Process Flow stage change.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/business-process-dynamics-365-crm-to-do-checklist-sequence\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone wp-image-33338\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/Business-Process-Checklist-2.png\" alt=\"Business Process Checklist (2)\" width=\"864\" height=\"216\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/Business-Process-Checklist-2.png 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/Business-Process-Checklist-2-300x75.png 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/Business-Process-Checklist-2-768x192.png 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/12\/Business-Process-Checklist-2-660x165.png 660w\" sizes=\"(max-width: 864px) 100vw, 864px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Recently while working on a customer requirement, we came across a scenario where we needed to trigger a plugin on change of Business Process Flow stages. So, in the pursuit of finding a solution, we came across workflow and process stage entities, but they were not very useful in this scenario. Our requirement was\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2022\/12\/how-to-trigger-the-plugin-on-any-business-process-flow-stage-change-and-update-custom-entity-record-related-to-the-active-stage\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2361],"tags":[226,2648],"class_list":["post-33407","post","type-post","status-publish","format-standard","hentry","category-technical","tag-business-process-flow-dynamics-365","tag-business-process-flow-stage"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/33407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=33407"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/33407\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=33407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=33407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=33407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}