In a previous article "Script Libraries: Reusing Client Side Code Between Screens" I discussed how Sage CRM will automatically add any JavaScript file that you add to the custom folder into the header of every page produced by the system.

It is a good idea when adding script libraries to use your own JavaScript namespace reference.

For example the code in my file 'companyrules.js' looks like this

var CompanyRules = CompanyRules || {};

CompanyRules.addGoogleSearch = function () {
var fcomp_name = crm.fields("comp_name");
var strName = fcomp_name.text();
var strLink = "<A HREF=";
if (fcomp_name.getMode() == "view") {
fcomp_name.text(strLink + escape(strName) + " target=blank>" + strName + "</a>")

CompanyRules.addStatusWarning = function () {
if (crm("comp_status").value() == "Closed") {

I have created an object called CompanyRules that will act as my namespace. And I have created two methods within that called 'addGoogleSearch' and 'addStatusWarning'.

Because the code is included in a script library that is automatically added to the page we can test this directly in a browser's console.

When I use the code in the library file by adding a script to the Custom Content block of a screen e.g. CompanyBoxLong if would look like this;

//function script goes here

I think there are a number of advantages to placing your code in namespaces.  The main global namespace is pretty crowded with lots of functions and objects like CurrentUser or GetKeys(), and we have other libraries being used.  Not only do we have the Sage CRM script libraries but also the jQuery library and you may have also referenced the Fusion Widgets library.  This means that the global namespace is far from being a clean slate.  There are numerous existing properties with which you risk collision.

Another benefit is the we can produce very simple to use public methods and properties which help produce well structured code.   Within the objects we create in our namespace we can add private properties and methods before returning an object.  We essentially can make the internal script as complex as we need while masking that from the way it is used in the interface.

I am sure that you have your own thoughts on this and I would welcome your comments below.