{"id":2664,"date":"2017-01-17T07:15:06","date_gmt":"2017-01-17T07:15:06","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2664"},"modified":"2017-01-17T07:36:46","modified_gmt":"2017-01-17T07:36:46","slug":"server-side-execution-of-business-rules","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2664","title":{"rendered":"Server Side Execution of Business Rules"},"content":{"rendered":"<p>Recently we hosted a free webinar on Dynamics 365 Business Rules. During webinar, there was one question regarding server side execution of the business rule: \u201cWill it execute during create request\u201d? And I answered it depends. If you are setting any field which has business rule associated with it, it will fire, but if you are not using that field business rule will not fire. We thought of writing a quick article to demonstrate this. So here is what we need:<\/p>\n<p>1. A business rule associated to some field which we are going to set in our code.<br \/>\n2. A sample console application to crate entity record using CRM SDK<\/p>\n<p>Before jumping into action make sure you meeting following requirements:<\/p>\n<p><strong>Prerequisite:<\/strong><br \/>\n1. Dynamics 365 Trial (You can refer our earlier article to setup dynamics 365 trial <a href=\"https:\/\/himbap.com\/blog\/?p=2503\">here<\/a>)<br \/>\n2. You should have windows identity foundation installed in\u00a0your machine.<br \/>\n3. Downloaded latest Dynamics 365 SDK<br \/>\n4. Visual Studio 12 or later with .Net 4.5.2 or later framework installed<\/p>\n<p>So let\u2019s get started:<br \/>\n<strong>Setting up Business Rule for Demo<\/strong><br \/>\nIn our business rule we are going to use account entity and we want to implement following logic:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2665 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule-300x235.png\" alt=\"serversidebusinessrule\" width=\"300\" height=\"235\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule-300x235.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule.png 364w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>let\u2019s say if user will select Consultant under relationship type, we will setup Consulting under Industry drop down and will setup it\u2019s consulting sic code under SIC code field. In our business rule we need to use two actions if type of customer is consultant. As you can see in following screenshot:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2666 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule1-300x142.png\" alt=\"serversidebusinessrule1\" width=\"300\" height=\"142\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule1-300x142.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule1-624x295.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule1.png 910w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You can create this business rule by following our <a href=\"https:\/\/himbap.com\/blog\/?p=2541\">earlier articles <\/a>for business rules. Make sure to select Entity under scope and activate business rule once it is completed.<\/p>\n<p><strong>Setting up console application<\/strong><br \/>\nOur business rule is ready, now we need to develop our console application using following steps:<\/p>\n<p>1. Start Visual Studio and select console application:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule2.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2667 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule2-300x213.png\" alt=\"serversidebusinessrule2\" width=\"300\" height=\"213\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule2-300x213.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule2.png 618w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>2. Now add reference of the following .Net and Dynamics 365 assemblies :<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule3.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2668 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule3-268x300.png\" alt=\"serversidebusinessrule3\" width=\"268\" height=\"300\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule3-268x300.png 268w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule3.png 360w\" sizes=\"(max-width: 268px) 100vw, 268px\" \/><\/a><br \/>\nAlso add following .net assemblies:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSystem.Runtime.Serialization\r\nSystem.configuration\r\nSystem.ServiceModel\r\n<\/pre>\n<p>3. Got to App.config file and add following connection string:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessruleupdate4.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2680 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessruleupdate4-300x98.png\" alt=\"serversidebusinessruleupdate4\" width=\"300\" height=\"98\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessruleupdate4-300x98.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessruleupdate4-624x203.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessruleupdate4.png 641w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<strong>** Make sure to change underlined string based on your organization details.<\/strong><\/p>\n<p>4. Open Program.cs and add include following assemblies:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System.Configuration;\r\n\r\nusing Microsoft.Xrm.Sdk;\r\n\r\nusing Microsoft.Xrm.Tooling.Connector;\r\n<\/pre>\n<p>5. Now add following sample code under Main method<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\n            CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(ConfigurationManager.ConnectionStrings[&quot;Dynamics365&quot;].ToString());\r\n            IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;\r\n\r\n            \/\/create request\r\n            Entity account = new Entity(&quot;account&quot;);\r\n            account[&quot;name&quot;] = &quot;Server Side Business Rule Example1&quot;;\r\n            account[&quot;address1_city&quot;] = &quot;Delhi&quot;;\r\n            service.Create(account);\r\n\r\n            Console.WriteLine(&quot;Record created, press Enter to close window !!&quot;);\r\n            Console.ReadLine();\r\n<\/pre>\n<p>6. Press F5 to start execution it should show us following message once create request is completed:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule5.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2670 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule5-300x89.png\" alt=\"serversidebusinessrule5\" width=\"300\" height=\"89\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule5-300x89.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule5.png 611w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>7. Now let\u2019s go to Dynamics 365-&gt; Accounts and check for our new account record. If we will open this new record we should see something like below:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule6.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2671 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule6-300x148.png\" alt=\"serversidebusinessrule6\" width=\"300\" height=\"148\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule6-300x148.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule6-624x308.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule6.png 691w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You can see here Relationship Type field is blank because we are not setting it yet and Industry and SIC code field is also blank.<br \/>\n8. Now let\u2019s update our create request to use a different account name and set our Relationship Type field as well. Press F5 to execute updated code.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\r\n            CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(ConfigurationManager.ConnectionStrings[&quot;Dynamics365&quot;].ToString());\r\n            IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;\r\n\r\n            \/\/create request\r\n            Entity account = new Entity(&quot;account&quot;);\r\n            account[&quot;name&quot;] = &quot;Server Side Business Rule Example2&quot;;\r\n            account[&quot;address1_city&quot;] = &quot;Delhi&quot;;\r\n           \r\n            account[&quot;customertypecode&quot;] = new OptionSetValue(2);\r\n            service.Create(account);\r\n\r\n            Console.WriteLine(&quot;Record created, press Enter to close window !!&quot;);\r\n            Console.ReadLine();\r\n<\/pre>\n<p>9. Once record is created, again go back to Dynamics 365 and open new record. We should be able to see like following:<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule7.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2672 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule7-300x112.png\" alt=\"serversidebusinessrule7\" width=\"300\" height=\"112\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule7-300x112.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule7-624x233.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/01\/serversidebusinessrule7.png 709w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Here we can see industry and sic code field is also set because of our server side business rule execution.<\/p>\n<p>So as we discussed above server side business rule will also execute only when we are changing field which has business rule associated with it. Hope it will help someone !!<\/p>\n<p>Stay tuned for more Dynamics 365 contents !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently we hosted a free webinar on Dynamics 365 Business Rules. During webinar, there was one question regarding server side execution of the business rule: \u201cWill it execute during create request\u201d? And I answered it depends. If you are setting any field which has business rule associated with it, it will fire, but if you are not using that field&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2664\">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":[21],"tags":[430,432,404,433,431],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2664"}],"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=2664"}],"version-history":[{"count":16,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2664\/revisions"}],"predecessor-version":[{"id":2689,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2664\/revisions\/2689"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}