Custom Xrm Script and Business Rules on the same Field in Dynamics CRM 2013

With the advent of Microsoft CRM 2013 in came business rules, a wonderful feature of the new CRM version. However with new implementations, there would be always some new confusions creeping up. Well in many training sessions on CRM 2013, i keep getting the question – “Will my onchange handlers and business rules work at the same time on the same field?”.

The best way to get the answer is to try out only. So let’s do a small test which i myself faced in one of my implementations. I was faced with a scenario where I had to to do whole lot of stuffs on change of field including clearing out certain other field values. All of them but clearing stuff was possible with business rules. So had to write a custom onchange event handler for the field. And yes both the business rules and my custom code triggered. Let’s see this with a simple example.

In the contact entity, for the email field I have a simple business rule which make the email field mandatory as soon as First Name field is entered/ changed by the user. Also for experimental purpose, I will register on on-change of First Name, to show a alert. Check for the screenshots below.

Screen 14

Screen 16

 

 

Now when I changed the First Name for a new Contact, first of all my custom jScript code fired. When I closed the alert by clicking OK, the Email field was set to Business Required by the Business Rule.

Screen 17

Screen 18

However remember the following MSDN Documentation – “When you set a field value by using a business rule, any OnChange event handlers for that
field will not run. This is to reduce the potential for a circular reference, which could lead to an infinite loop. ”

So when you are designing business rules and customer onchange handlers for your implementations, please do take all this into consideration.

Hope this helps!

Advertisements

Author: Debajit

I am a Dynamics CRM Most Valuable Professional (MVP) with 10 years of experience in Microsoft .NET Technologies and 7 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

2 thoughts on “Custom Xrm Script and Business Rules on the same Field in Dynamics CRM 2013”

  1. This is not a surprise to me. I think you have misread the intention of the documentation. If you have a Business Rule that causes a field to be changed (ie sets the field’s value), then this update does not *trigger* an onChange event. If there are other Business Rules using that field in the Conditions, then those rules *are* re-evaluated to see if anything changes that matters.

    1. Hi
      I totally agree to your point and actually i had my understanding cleared few weeks after I posted the blog. However I totally forgot to modify the blog content after that.

      Sincere thanks to you for your comment as it reminded me. I will get done with the modifications of this post pretty soon so that people are not misinformed.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s