We’ve already shown you how to perform field validations to control field visibility and field requirement levels with Business Rules in Microsoft Dynamics CRM 2013. In this blog, we will discuss how to create more advanced business rules using if then statements.
Business rules allow you to check for one or more conditions and then take one or more actions if the conditions are met. Consider the fact that although multiple conditions can be defined in each business rule, each condition will be processed using the AND operator. If we create a single business rule with two conditions, both conditions must be met in order for the action to be executed.
So what if we need to check for one condition OR another? Currently, we would need to create two separate business rules to accomplish this—one to check for each condition and take the appropriate action. The only exception to this constraint is when your condition is referencing an option set field. When setting a condition for an option set field you can choose more than one option set value in the same condition statement and the business rule will process this using the OR operator.
The following example will help show you how this all works. Let’s say we have a requirement to automatically populate the Total Holiday Budget field for each account. Each account record is identified with an Account Size and an Account Category value, which together will be used to determine the total holiday budget amount. The following table displays the desired end result for each combination of account size and account category.
Category Preferred Account
Category Standard Account
Category Non-Standard Account
Holiday Budget Amount
|Account Size Large|
|Account Size Medium|
|Account Size Small|
It helps to first lay out the required logic using pseudo code.
- If Category = Null OR Account Size = Null, then Total Holiday Budget = 0 and is hidden on the form
- Else If (Category = Preferred Account) OR (Account Size = Large AND Category = Standard Account), then Holiday Budget = $2,500 and is displayed on the form but is read-only
- Else If ((Account Size = Medium OR Small) AND Category = Standard Account), then Holiday Budget = $1,000 and is displayed on the form but is read-only
- Else If Category = Non-Standard Account, then Holiday Budget = 0 and is hidden on the form
Now let’s take a look at how we can accomplish our requirement with business rules!
1. The first line of pseudo code logic requires two business rules. Each business rule will check for one condition OR the other. Both business rules will have the same actions.
2. The second line of pseudo code logic will require two business rules. Each business rule will check for one condition OR the other. Both business rules will have the same actions.
3. The third line of pseudo code logic only requires one business rule. Even though we are checking for a condition where Account Size = Medium OR Small, since this is an option set, the business rule can accommodate it with one condition.
4. The fourth line of pseudo code logic also only requires one business rule.
One more important thing you need to understand about the example above is that setting values in read-only fields is problematic. If you use a business rule to “lock” a field for which you have set a value, the lock will prevent the value from being saved in the record. The same result can be expected if you create a business rule to set the value of a field that is already set to read-only on the form. The solution to address this issue is to create a simple function in a web resource and apply it to the form’s OnLoad event.
So let’s see how to force the value to update in a read-only field.
1. First create the function in the web resource.
//Use this function to write to a read-only field
2. Next add the web resource to the form properties and add the function to the Form OnLoad event.
Let’s see the results!
Hopefully, this example will help you make the most of using business rules. As always, if you need assistance, the CRM experts at PowerObjects are here to help.