Dynamics CRM Model Driven Apps: Make use of addOnLoad and removeOnLoad

In this post, we will learn about how to use formContext.data.addOnLoad and formContext.data.removeOnLoad. Generally, formContext.data.addOnLoad function is totally the same with below function (register manually from form):

So the basic question that we will ask is why we need this API? For me, I will use this API if I have dynamic requirements. Last time, I have one friend that asked me about the complexity of the project he is doing. There are at least 3 instances of Dynamic CRM with different Solutions Installed on each layer.

In the above Picture, CRM C has dependencies in Solution 1 and Solution 2 (Managed Solutions). But the requirement for formOnLoad on Entity A in Solution 3 we need to call Method A and Method C only (removed Method B) which is not supported by current Implementation of CRM (Solutions changes only for Added Components). So how are we going to solve this implementation? 

First, you need to create 1 JavaScript like below (to simplify the process We just use 1 CRM environment but register 3 events to simulate the final result in CRM C):

var Blog = Blog || {};
Blog.Form = Blog.Form || {};
Blog.Form.Solution1 = Blog.Form.Solution1 || {};
(function() {
    this.formOnLoadFn = function(context){
        console.log('formOnLoad Solution 1');
    };
    this.form_OnLoad = function(context) {
       var formContext = context.getFormContext();
       formContext.data.addOnLoad(Blog.Form.Solution1.formOnLoadFn);
    };
}).apply(Blog.Form.Solution1);
Blog.Form.Solution2 = Blog.Form.Solution2 || {};
(function() {
    this.formOnLoadFn = function(context){
        console.log('formOnLoad Solution 2');
    };
    this.form_OnLoad = function(context) {
       var formContext = context.getFormContext();
       formContext.data.addOnLoad(Blog.Form.Solution2.formOnLoadFn);
    };
}).apply(Blog.Form.Solution2);
Blog.Form.Solution3 = Blog.Form.Solution3 || {};
(function() {
    this.formOnLoadFn = function(context){
        console.log('formOnLoad Solution 3');
    };
    this.form_OnLoad = function(context) {
       var formContext = context.getFormContext();
       formContext.data.addOnLoad(Blog.Form.Solution3.formOnLoadFn);
       formContext.data.removeOnLoad(Blog.Form.Solution2.formOnLoadFn);
    };
}).apply(Blog.Form.Solution3);

In the above coding, we just want to simulate that there are 3 JavaScript. Solution 1, 2, and 3 have 3 events registered on formOnLoad. If you take a look closely at Blog.Form.Solution3.form_OnLoad, you will see that we remove Blog.Form.Solution2.formOnLoadFn to achieve the requirement given. Below is how I register those events:

When You load the form, this is the result:

One thought on “Dynamics CRM Model Driven Apps: Make use of addOnLoad and removeOnLoad

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.