What I really hate about CRM development is that sometimes you need to duplicate the code both in UI and on a server side.

For example, when you need to calculate something on a form. In this case, you have two options to choose:

  1. Writing a JavaScript that will make calculations on a form and change values accordingly or
  2. Writing a Plugin that will calculate these values upon data saving.

Both the approaches have their advantages and disadvantages. One of the advantages of using a JavaScript is that you can ensure user instant feedback on what value s/he receives after all. While the plugin guarantees the calculated values stored in a database are correct.

At the same time, integration or other service calls will not execute the JavaScript or it may fail for any reason (for example, another broken JavaScript, or browser failure). Besides, I do not trust JavaScript to execute business logic and calculations because it does not guarantee that everything will work as it should with the next browser update. On the other hand, a user instant feedback is very important, because it is easier to use the system when you can see the result without the need to save data every time. Nor less important is the user input verification on the fly, so user don’t need to go back and correct data after failed to save.

So, the best way out in this situation would be to develop both JavaScript and Plugin. However, such approach results in a code duplication and increases the change management complexity.

How Microsoft CRM 2013 can change the game

As you may know, Microsoft CRM 2013 has introduced a new feature called Actions. This great feature allows you to add custom web service messages and process them with a plugin (one possible but not the last use case of this feature) and, at the same time, to call these messages from a JavaScript, therefore, giving an opportunity to bridge JavaScript and C# code.

Summing up the above said, as the Actions can be executed both from javascripts and from plugins it becomes possible to avoid the problem of code duplication by writing the code on one side only. And that could be the solution we need.

How Actions can help to move all business logic into a managed code

I’ve decided to write some C# interfaces that define often used patterns in Plugins and JavaScript, which are:

After that, I wrote a wrapper that finds all classes implementing these interfaces in a current assembly and executes them accordingly, and then, as Actions enable interaction between JS and plugins, I wrote a JavaScript executing the following procedure:

OnSave event is not required here because all calculations and validations occur during OnChange events processing. This insures quick response to the customer’s data and actions.

Finally, I wrote a plugin that executes Business Rules implementation on a Server side:

So, as you can see, the only thing we need is just to change and support Business Rules classes and they will work on Server and UI side with no extra efforts.

Let’s take a closer look to Pros and Cons of this solution.


  • Performance loss for small JS without server interaction.

If you need just a small script that works with the form data only, you’d better write it in a pure JS as in this case it ensures faster logic performance. But once you add JS interaction with the server (request additional data, check user role names, etc.), it is the server-side logic that begins to win the performance competition.


  • To enable functionality you add just one javascript to OnLoad form and that’s all. No OnChange, OnSave events, etc. Everything’s done automatically.
  • No need to describe the behaviour, just logic.
  • No code duplication. You write it once and it works automatically both on UI (JavaScript) and server (Plugin) sides allowing you to benefit the advantages of both approaches.
  • Easy change management as you write the code for server-side only, so all changes are done in one place. No hidden dependencies.
  • Same pattern for different developers. All developers will use the same pattern to write code, so it should also lower maintenance costs.

To find more information, please, view the presentation.



MS CRM 2015 announced that it would support server side Business Rules, finally, they understand that :). Unfortunately, it will support limited functionality and will not support custom code. But I think in future releases they will remove obsolete Plugin approach