Back to Basics # 18: Business Rules  with Entity Scope and Custom Code Execution

Introduction:

In Dynamics 365 CRM or Power Platform, updates in entity data by using service call /SDK which do 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 contact entity by using SDK Call through code which invokes business rule 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.

Step 2:

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

Step 3:

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

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. Logic here is if the field Birthday have value, then field Vaccination becomes mandatory and Discount field will be shown save business rule and activate and then publish the solution as shown in the below figure.

Step 5:

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

Step 6:

After Step 5, open a console app of type C#.net in Visual studio and establish connection with  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

Step 7:

After Step 6, open contact record in crm Environment and copy contact id of James M contact record in CRM Environment from url also observe Birthday field is empty as shown in the below figure

Step 8:

After Step 7, use the below code and trying to update Birthday birthdate field by passing it to contact entity and with SDK  call to update 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

Step 9:

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

Step 10:

After Step 9, navigate back to crm environment and open/ refresh James M record and see birthdate got populated and 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

as shown in the below figure

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. Entity
  5. Give name of conditions and Actions names in a meaningful way.
  6. Make sure to configure CRM Environment console app to use for 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 only fires.

3 thoughts on “Back to Basics # 18: Business Rules  with Entity Scope and Custom Code Execution

  1. Pingback: Rewind September 2021 – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s