FujiForty - Calling Script Includes from Scopes

With the introduction of application scopes there are a couple ways we can instantiate script include classes depending on the scope our process is running in and the scope of the script include.

For process and script includes that reside in the same scope, there's really no change needed.

var cls = new MyClass();

When calling script include classes from a different scope we need to specify the scope namespace prefix. First we need to make sure we have the appropriate access to the record. 

The API name can be found in the script include record. It is derived from the application scope name and the script include name. To instantiate this class from a different scope, the syntax is:

var cls = new x_cavu_app.MyClass();

Both of these examples will work as long as you are running from a predictable app scope. It's also helpful to know that you can always use the full API name, even if you are calling it from the same scope. I prefer to never use the shortened API format. This way I never have to worry about what scope it's being called from, especially if I ever copy a calling script from one scope to another.

Including API namespace prefix also applies to calling any script include in the global scope that is access from outside of global.

var myObj = new global.JSON().decode('{name:"someJSONstring"}');

This also applies when referencing classes to create class extension.

var MyAppAJAX = Class.create();
MyAppAJAX.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
  ...
});

And also when calling the AJAX class from the client.

var ga = new GlideAjax("x_cavu_test.MyAppAJAX");