Register events on Dynamics 365 forms – Did you know library name doesn’t matter?

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at

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.


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.


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.


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


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 or reach out to us at

4 thoughts on “Register events on Dynamics 365 forms – Did you know library name doesn’t matter?”

  1. Thanks for the blog Debajit.
    This happens because when you reference Javascript on any HTML page, the client (browser) does not differentiate between the files, just takes the text of the javascript.
    Its like telling the browser use JavaScript file1, JavaScript file 2 and JavaScript file 3 on this HTML page. Browser just copies and dumps the content of the JavaScript from each file into the HTML.

    Even though we are adding resources from the CRM form properties, the underlying concept is the same.
    That is why it is always a good idea to use namespace in your JavaScript file.

    1. Hi Hufeza
      Thanks for reading my blog post.
      Yes that’s easy to understand for developers starting with web technologies and moving to CRM. But developers starting with CRM only tend to miss sometimes these basic concepts..


      1. True Debajit.
        This is where namespacing in JavaScript helps, should be followed as best practice I feel


        P.S It’s Huzefa, not Hufeza 🙂

Leave a Reply