Script includes

Script includes are at the heart of scripting in ServiceNow, and are arguably the most commonly used when it comes to writing code. Script includes are used to hold classes of code, and for a lot of the backend script used by the ServiceNow platform.

When creating your script include, you first need to give it a name. This name will be important, as it will be used in other code to call the methods in your script include.

Ensure that the name you choose for your script include does not contain spaces in it. It is best to use underscores to separate words in the name.

Once you enter a name for your script include, you will notice that the API Name and the script fields are populated. The API name is read-only, and given based upon the name of the application this script include is being created in, followed by the name of the script include itself.

The script field is populated with some introductory script creating a class for this script include, taking into account the name of the script include. As an example, if we named our script include script_utils, we would be given the following script:

var script_utils = Class.create();
script_utils.prototype = {
initialize: function() {
},

type: 'script_utils'
};

This gives us the basic code to create a script include class that can be called from the server side. One of the first questions to ask when creating a script include is whether the script will be called from the server side only, or from the client side. This is because if we are going to be calling the script include from the client side (for example, for an AJAX call), we need to tick the Client callable checkbox, and in doing this, you will also notice that the code changes, too.

For our example, this will change the code to the following:

var script_utils = Class.create();
script_utils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

type: 'script_utils'
});

This changed script allows the script include to inherit the AbstractAjaxProcessor. Without this, AJAX calls will not work, so it is important to remember this if the script include will be called on the client.

Although this is the starter code that ServiceNow provides for you, it is not necessary to create a class in a script include. You can also simply create a function to call in a script include when calling server-side script.

Let's look at a short example of how that can be done:

function script_include_test() {
return 'Test Complete';
}

This is a simple function that will just return the string of text, but can be called simply as a standalone function, rather than creating a whole class.

Let's now have a look at creating a script include with a class and methods. We can add as many methods as we like to our script include:

var script_utils = Class.create();
script_utils.prototype = {
initialize: function() {
},

testMethod: function testMethod() {
return 'Method completed successfully';
},

type: 'script_utils'
};

Our testMethod method will return a string when called on the server side. To call it, we use the line of code as follows:

new script_utils().testMethod();

We can also assign this line of code to a variable or use it for a condition check if the method returns true or false.

Script includes can also call each other and methods contained within them. When building up the bulk processing of server-side script, it is usually best to consider script includes for the job. Looking at the existing script includes that come with the ServiceNow platform is a good way to enhance your knowledge of writing script includes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.243.184