Tag Archives: CRM 2013. Business Rule

Using Business Rules for Calculated Fields in CRM 2013

Business Rules are a viable alternative to JavaScript for basic field calculations, such as the below example where I want Line Total to be calculated based on Unit Amount x Qty:

image

To automate this simple calculation I configured 3 Business Rules.  Firstly, the rule that sets Line Total =  Unit Amount x Qty (when both those fields are populated):

image

And two additional Business Rules to clear the Line Total field once either of the input fields are cleared:

image 

And:

image

I describe this as a JavaScript replacement and it is.  However, that doesn’t mean either JavaScript or a Business Rule is the right solution to this automation requirement.  They both have the same weakness, they only fire when the data is changed through the CRM form.   Understanding when Business Rules fire is easy, just think of them as JavaScripts.  Behind the scenes the Business Rules you author are translated into JavaScript so there really is no difference.    Implementing a calculation via just a JavaScript or Business Rule leaves your data integrity exposed to updates via Bulk Edit, Data Import, Mobile Express, API and Workflow updates where updates to your fields won’t trigger the recalc of the calculated field.   For that reason, I would meet this requirement with a combination of a Business Rule and a Plug-in.  The Business Rule provides a real time calculation experience for the user, the Plug-in ensures the calculation happens regardless of the source of the update.    Wondering whether a simple Real Time Workflow could do the job of the Plug-in?  The answer is no, the workflow designer doesn’t provide the ability to define this type of calculation.

A few other things to know about Business Rules…

Once a field is referred to within a Business Rule that association becomes part of the field’s properties, visible from the form designer:

image

  • When setting a field value you can only draw from fields of a similar data type.   In the example above I was able to set a Currency field based on a  Floating Point field and another Currency field.  But if I want to set a Text field based on an Option Set or Lookup field I’m out of luck.
  • When setting a field you can’t draw on any context information such as the current user or current date time.   I tried to workaround this be setting fields based on Modified On and Modified By but those values are only as current as the last save so are do not reliably give you ‘now’.
Advertisements

CRM 2013 New Features: Business Rules

One of the new toys we get with CRM 2013 is the first release of Microsoft’s Business Rules engine.   Think of this release as a configuration-based replacement for basic field manipulation javascript.  

You get a simple UI where you can define Business Rules that will be executed by the CRM form’s OnLoad and OnChange events.  The rules are defined within each entity inside your solution file:

image

Each rule is comprised of Conditions and Actions (and there is a space to include some comments).  Here’s an example:

image

The Conditions are similar to what you will be used to with CRM’s workflow designer but Microsoft have taken a different approach to the configuration experience. When evaluating fields in your conditions you can check for nulls / not nulls and you can compare the entered value with a hard coded value, a calculated value or with another field:

image

The actions available are primarily related to fields:

image

You can also write Business Rules that perform validations, block record updates and display errors the user.  Here I tell the user off once they enter an Est. Revenue value:

image

This surfaces in the UI as follows:

image

Other things to know:

  • The rules need to be activated before they take effect (similar to workflows)
  • You can specify whether the rule applies to all forms or specific forms
  • The rules fire in the new mobile client for iPad and Windows 8
  • The rules are client-side rules, they do not fire during API calls, data import, etc.    Microsoft’s vision is to add that support in a future release.
  • The rules do not support show/hide of Tabs and Sections.  You either need to mention each field individually in your Business Rule or use javascript.
  • If your rule hides a field then CRM moves fields up to close the gap
  • You need to consider the inverse of your rule.  i.e. in my example I tell CRM what to do when the Est. Revenue field contains data.  I don’t tell it what to do when the Est. Revenue does not contain data.  I need to write a second Business Rule for that.
  • JavaScript runs before Business Rules
  • String comparisons are case sensitive

 

Fun feature, I like it!