Server Side Execution of Business Rules

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: “Will it execute during create request”? 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:

1. A business rule associated to some field which we are going to set in our code.
2. A sample console application to crate entity record using CRM SDK

Before jumping into action make sure you meeting following requirements:

Prerequisite:
1. Dynamics 365 Trial (You can refer our earlier article to setup dynamics 365 trial here)
2. You should have windows identity foundation installed in your machine.
3. Downloaded latest Dynamics 365 SDK
4. Visual Studio 12 or later with .Net 4.5.2 or later framework installed

So let’s get started:
Setting up Business Rule for Demo
In our business rule we are going to use account entity and we want to implement following logic:

serversidebusinessrule

let’s say if user will select Consultant under relationship type, we will setup Consulting under Industry drop down and will setup it’s 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:

serversidebusinessrule1

You can create this business rule by following our earlier articles for business rules. Make sure to select Entity under scope and activate business rule once it is completed.

Setting up console application
Our business rule is ready, now we need to develop our console application using following steps:

1. Start Visual Studio and select console application:

serversidebusinessrule2

2. Now add reference of the following .Net and Dynamics 365 assemblies :

serversidebusinessrule3
Also add following .net assemblies:

System.Runtime.Serialization
System.configuration
System.ServiceModel

3. Got to App.config file and add following connection string:

serversidebusinessruleupdate4
** Make sure to change underlined string based on your organization details.

4. Open Program.cs and add include following assemblies:

using System.Configuration;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Tooling.Connector;

5. Now add following sample code under Main method


            CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(ConfigurationManager.ConnectionStrings["Dynamics365"].ToString());
            IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

            //create request
            Entity account = new Entity("account");
            account["name"] = "Server Side Business Rule Example1";
            account["address1_city"] = "Delhi";
            service.Create(account);

            Console.WriteLine("Record created, press Enter to close window !!");
            Console.ReadLine();

6. Press F5 to start execution it should show us following message once create request is completed:

serversidebusinessrule5

7. Now let’s go to Dynamics 365-> Accounts and check for our new account record. If we will open this new record we should see something like below:

serversidebusinessrule6

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.
8. Now let’s update our create request to use a different account name and set our Relationship Type field as well. Press F5 to execute updated code.


            CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(ConfigurationManager.ConnectionStrings["Dynamics365"].ToString());
            IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

            //create request
            Entity account = new Entity("account");
            account["name"] = "Server Side Business Rule Example2";
            account["address1_city"] = "Delhi";
           
            account["customertypecode"] = new OptionSetValue(2);
            service.Create(account);

            Console.WriteLine("Record created, press Enter to close window !!");
            Console.ReadLine();

9. Once record is created, again go back to Dynamics 365 and open new record. We should be able to see like following:

serversidebusinessrule7

Here we can see industry and sic code field is also set because of our server side business rule execution.

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 !!

Stay tuned for more Dynamics 365 contents !!

About US
We at HIMBAP are a team of highly skilled Microsoft Dynamics 365/CRM experts, with extensive experience in developing solution for diverse domains, across verticals.Contact US if you are looking for Microsoft Dynamics 365/CRM implementation/Upgrade/Customization/Training help.

2 thoughts on “Server Side Execution of Business Rules

  1. Pingback: Server Side Execution of Business Rules - Microsoft Dynamics CRM Community

  2. Pingback: Server Side Execution of Business Rules | Asif Huddani's Blog

Leave a Reply