Business Rules With Entity Scope And Custom Code Execution

Introduction

In Dynamics 365 CRM or Power Platform, do updates in entity data by using service call /SDK which makes changes in server-side, then server-side business rules will fire i.e., business rules with scope Entity will fire. As a business scenario, we are going to see updates in the contact entity by using SDK Call through code that invokes business rules with Entity Scope.

Step 1

Login to the required Dynamics CRM/ Power Apps environment using URL make.powerapps.com by providing username and password and go to solutions as shown in the below figure.

Business Rules with Entity Scope and Custom Code Execution

Step 2

After step 1, the select custom solution here selects ContactCustomizations custom solution as shown in the below figure.

Business Rules with Entity Scope and Custom Code Execution

Step 3

After step 2, in the ContactCustomizations custom solution open Contact Entity and then click on the Business Rules tab and then Click on Add business rule as shown in the below figure.

Business Rules with Entity Scope and Custom Code Execution

Step 4

After step 3, create a business rule with scope as Entity and name business rule with Show Business Required Fields in Contact And Set Visibility. The logic here is if the field Birthday has value, then field Vaccination becomes mandatory and Discount field will be shown. Save the business rule and activate and then publish the solution as shown in the below figure.

Business Rules with Entity Scope and Custom Code Execution

 

Step 5

After step 4, create a record in contact entity in selected Dynamics CRM Environment as shown in the below figure,

Business Rules with Entity Scope and Custom Code Execution

 

Step 6

After step 5, open a console app of type C#.net in Visual studio and establish a connection with the respective CRM Environment by providing details of CRM Environment URL, user name, and password like below,

string conn = $@"
 Url = {url};
 AuthType = OAuth;
 UserName = {userName};
 Password = {password};
 AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;
 RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;
 LoginPrompt=Auto;
 RequireNewInstance = True";

as shown in the below figure,

Business Rules with Entity Scope and Custom Code Execution

Step 7

After step 6, open contact record in CRM Environment and copy contact id of James M contact record in CRM Environment from the URL. Also, observe Birthdayfieldisempty as shown in the below figure,

Business Rules with Entity Scope and Custom Code Execution

Step 8

After step 7, use the below code and try to update the Birthdaybirthdate field by passing it to the contact entity and with SDK call to update the record as shown below with code,

using(var svc = new CrmServiceClient(connection)) {
    DateTime birthDate = new DateTime(2015, 12, 20);
    //Pass contact id of contact James
    Guid contactid = new Guid("cf265bbd-e104-ec11-94ef-000d3a59b6ba");
    Entity contactentity = new Entity("contact") {
        Id = contactid
    };
    contactentity["birthdate"] = birthDate;
    svc.Update(contactentity);
    Console.WriteLine("Record updated successfully.");
    Console.WriteLine("Press any key to exit.");
    Console.ReadLine();
}

As shown in the below figure,

Business Rules with Entity Scope and Custom Code Execution

Step 9

After step 8, run the app and see the record got updated as shown in the below figure,

Business Rules with Entity Scope and Custom Code Execution

Step 10

After step 9, navigate back to CRM environment and open/ refresh James M record and see if birthdate got populated as well as making sure the respective business rule which was created in Step 4 fires and makes fields Vaccination as Business Required and Discount Field shows up in the form as shown in the below figure,

Business Rules with Entity Scope and Custom Code Execution

Note

  1. Please make sure you keep the required fields on Contact Form.
  2. Select fields for Actions as well as for Conditions without fail.
  3. Make sure to Activate Business Rule after save and give scope as.
  4. Publish all changes without fail.
  5. Give the name of conditions and Actions names in a meaningful way.
  6. Make sure to configure the CRM Environment console app to use for the Update operation.
  7. Take proper care to pass required fields as per the definition example here birthdate field name.

Conclusion

In this way, for any updates through custom code SDK, only business rules with Scope entity fires.