Server Side Execution Of Business Rules

Recently, we hosted a free webinar on Dynamics 365 Business Rules. During the 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, the business rule will not fire".

 Then, I 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 are meeting the following requirements.

Prerequisite

  1. Dynamics 365 Trial (You can refer my 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 the following logic.

  

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 the type of customer is consultant. You can see it in the following screenshot.

 

You can create this business rule by following our earlier article 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 the following steps.

  1. Start Visual Studio and select console application.



  2. Now, add reference of the following .NET and Dynamics 365 assemblies.



    Also, add the following .NET assemblies.
    1. System.Runtime.Serialization  
    2. System.configuration  
    3. System.ServiceModel  
  3. Go to App.config file and add the following connection string.

     

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

  4. Open Program.cs and include the following assemblies.
    1. using System.Configuration;      
    2. using Microsoft.Xrm.Sdk;      
    3. using Microsoft.Xrm.Tooling.Connector;   
  5. Now, add the following sample code under Main method.
    1. CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(ConfigurationManager.ConnectionStrings["Dynamics365"].ToString());     
    2. IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;     
    3. //create request     
    4. Entity account = new Entity("account");     
    5. account["name"] = "Server Side Business Rule Example1"; account["address1_city"] = "Delhi";     
    6. service.Create(account);     
    7.     
    8. Console.WriteLine("Record created, press Enter to close window !!"); Console.ReadLine();    
  6. Press F5 to start execution. It should show us the following message once the Create request is completed.



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

    You can see that Relationship Type field is blank here, because we are not setting it yet and "Industry" and "SIC Code" fields are 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 the updated code.
    1. CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(ConfigurationManager.ConnectionStrings["Dynamics365"].ToString());  
    2. IOrganizationService service = (IOrganizationService) conn.OrganizationWebProxyClient != null ? (IOrganizationService) conn.OrganizationWebProxyClient : (IOrganizationService) conn.OrganizationServiceProxy;  
    3. //create request     
    4. Entity account = new Entity("account");  
    5. account["name"] = "Server Side Business Rule Example2";  
    6. account["address1_city"] = "Delhi";  
    7. account["customertypecode"] = new OptionSetValue(2);  
    8. service.Create(account);  
    9. Console.WriteLine("Record created, press Enter to close window !!");  
    10. Console.ReadLine();  
  9. Once the record is created, go back to Dynamics 365 again and open new record. We should be able to see the following.
Here, we can see that "Industry" and "SIC Code' fields are also set because of our server side business rule execution.

So, as we discussed above, the server side business rule will execute only when we are changing the field which has business rule associated with it. Hope it will help someone !!

Stay tuned for more Dynamics 365 content !!

Up Next
    Ebook Download
    View all
    Learn
    View all