Quantcast
Channel: Microsoft Dynamics CRM
Viewing all articles
Browse latest Browse all 154803

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

$
0
0
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 “ formLoad ” which 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 www.xrmforyou.com or reach out to us at info@xrmforyou.com

Viewing all articles
Browse latest Browse all 154803

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>