Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Creating If Then Else Logic in CRM 2013 Business Rules

Post Author: Joe D365 |

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.

Background

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.

Example

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

$2,500

$2,500

0

Account Size Medium

$2,500

$1,000

0

Account Size Small

$2,500

$1,000

0

 

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

 

How To

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.

If Then Else Logic in CRM 2013

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
function ForceSubmit()
Xrm.Page.getAttribute("new_holidaybudget").setSubmitMode("always");
}

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.

Happy CRM'ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

4 comments on “Creating If Then Else Logic in CRM 2013 Business Rules”

  1. One thing for readers to keep in mind is that Business Rules run client side only, so modifications from workflow, bulk edit, ODATA, plugin, etc. will need to replicate the desired logic, as the Business Rules aren't invoked in those cases.

  2. Hi, I´m working with JS and Business Rules in the same form, in the JS I change some values (I use the force sumit in the last value), when I want to validate the information that is in the calculated attribute (by JS) the Business rule doesn´t work. Do you know how to make It works?

    Thanks!

    1. Hi Jon - my guess is a cache issue. I wonder if there is a way to refresh. We'll investigate a bit, but if you find anything pls post it here.

PowerObjects Recommends