Calling Namespace Javascript function in Dynamics CRM

I still see many people have some issue in understanding/calling namespace Javascript function.

Below pattern is a simple way of writing a Javascript in Dyanmics CRM, which most of the people are familiar with:

function MessagePopUp(executionContext)
{
 var formContext = executionContext.getFormContext();
 var email = formContext.getAttribute('emailaddress1').getValue(); 
 alert('Welcome mail sent to ' + email); 
}

Now, the namespace format, which is recommended by Microsoft. To avoid conflict within the functions name.

var Namespace = window.Namespace || {};
(
 function (){
 this.formOnLoad = function(executionContext)
{
 var formContext = executionContext.getFormContext();
 var email = formContext.getAttribute('emailaddress1').getValue();
 alert('Welcome mail sent to ' + email);
}
}
).call(Namespace);

In the above code we have declared the variable as “Namespace”. The same should be called at the end of the function as .call(Namespace).

this.functionName = function(executionContext) /* In namespace notation javascript function should be called using this keyword, */

For calling a above namespace notation javascript, we need to call it as Namespace.formOnLoad on the event handler.

Figure 1: For calling formOnLoad function we need to use Namespace.formOnLoad
Figure 2: Screenshot from Event Handlers

Save and Publish the code.

Refreshing the contact entity page and we will get the message pop-up on form onload.

Figure 3: Message Pop-Up

Hope this blog help in understanding Namespace Notation Javascript!!