Monday, October 7, 2013

CRM 2013 Business Rules vs. JavaScript - Who Wins?

A Business Rule in Dynamics CRM 2013 is a piece of functionality that allows system administrators a quick and easy way to incorporate some basic business logic and form manipulation into their organization without having to know JavaScript. Through a GUI in the customizations area, administrators can create simple If/Then type logic to set form field values, make fields required, hide & show fields, enable & disable fields or attach error messages to fields. One of the best parts is that any logic you define here can be applied to standard forms and the new Quick Create forms in the web client as well as forms in the new tablet client.

So what is happening here? The logic defined in the Business Rule is translated into JavaScript and applied to the form so that rules are checked both when the form loads and during the field's OnChange event.

But what happens when both a Business Rule and some JavaScript is applied to the same event on a field? As it turns out, the Business Rule comes out on top. I've put together a few illustrations.

I've created 2 Business Rules. The first shows an error message when the Account Number field equals 123456. The second sets the Account Number field value when the Account Name equals "New Account".



I've also created some JavaScript and attached it to the OnChange event of the appropriate fields to mimic the behavior of the Business Rules.


To test the first I'll set the Account Number to 123456...


The Business Rule won.

Next I'll set the Account Name to "New Account"...


The Business Rule won again.

So it would see that the 2 don't coexist well together. So for those of you out there that write JavaScript and end up responding to a report that functionality is no longer working, double check to see if any Business Rules were put in place that might be conflicting with your code.

But on the bright side, the 2 can work together. If we change the second rule so that instead of setting the Account Number's field value rather to display an error message, we can see that we get both results.