Gotcha! Does Library name really matter while registering event handler in Dynamics 365

Registering an event handler for your form events or field events – Probably the most mundane stuffs that you would perform everyday if you a CRM consultant. But sometimes the simplest things have the greatest mysteries to unfold.

Now here I was in a training session emphasizing the importance of namespaces while you write your JavaScript files. And I was explaining how can putting the same method name in multiple files can actually lead to different event handler being called for your event instead of the desired event handler registered.

And then I get this question – “How is that possible? After all we specify the library name while specifying the event handler. Isn’t it?” Well the participants were pretty experienced in CRM and honestly I was also into split thought after I heard.

image

After all from the basic concept of the Javascript I know,  the first function which matches the name will be called. The best way to know something is trying and that’s what we did.

So I created two files here

file1.js with the below content

// JavaScript source code
function formLoad() {
    var alertStrings = { text: "Alert from file1.js", title: "Alert" };
    var alertOptions = { height: 200, width: 300 };

    Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
}

file2.js with the below content

// JavaScript source code
function HelloWorld() {
    var alertStrings = { text: "Alert from file2.js", title: "Alert" };
    var alertOptions = { height: 200, width: 300 };

    Xrm.Navigation.openAlertDialog(alertString, alertOptions);
}

Now comes the important part.

I go ahead to the form properties of the Lead form and register the below event handler.

image

Observe here carefully. I have put the file name to new_/scripts/file2.js and the function name I have put is “formLoadwhich is actually not there in the file2.js at all.  Also I have included the both the files in the form libraries as shown in the below screenshot.

image

Now when I open a lead record – Gotcha! I can see the alert

image

Even though I was handling participants who were pretty much experienced but believe me I could literally find an absolute taken aback expression once the alert dialog popped up. In the world of Power Apps and Azure, sometimes the tiniest and mundane stuffs actually leaves us in awe.

So this is one stuff. But what we get from it. Well now you understand that if we have the same method name in both file1 and file2 and you registered an onload event for the method in file1, there is no guarantee which method of which file gets called.

And that’s the whole I repeatedly stress on the use of namespaces while writing JavaScript files.

-Debajit Dutta

(Dynamics MVP)

For consultation/ training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Advertisements

One thought on “Gotcha! Does Library name really matter while registering event handler in Dynamics 365

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