JavaScript and Business Rules

I haven’t blogged about JavaScript or code changes for some time! Probably because in Microsoft Dynamics 365 I mainly use a low code approach these days. But today I had a small challenge with some code and thought the solution I found might be useful to others ….. my challenge was that my business rules weren’t being triggered after a field had been changed using JavaScript.

I guess this must be a common problem! But it just happens to be a problem I haven’t had before.

In my scenario I had a business rule that was hiding / showing fields on my account form. The fields were visible (or not) based on a custom account type field. The business rule worked perfectly when I manually changed the account type on my form. But I also had some code which changed the value of my account type. Changing my account type using JavaScript worked but didn’t trigger my business rule. This made me sad!!

I could have re-created the logic from my business rule in JavaScript on my account form. But I really didn’t like the idea of creating more code!

So instead I added one extra line of code to fire the onchange event in my account type field. Which in turn triggered the business rule. Meaning I didn’t have to write more code. This made me happy!!

So you’d add something like <<FormContext>>.getAttribute(“myattribute”).fireOnChange(); (As in my example below.)

 

var newValue = “Bla bla bla”;

var accountType = context.getAttribute(“new_accounttype”);

accountType.setValue(newValue);

accountType.fireOnChange();

 

As you can see this was a very simple change. If you frequently mix JavaScript and Business Rules I’m going to guess you’ll already be familiar with this “workaround”. But if like me you’d not stumbled on this problem before I do hope this short blog post will prove useful. Enjoy.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s