Calling a Function Within the Same Script Include in ServiceNow
- davidyang88
- Mar 20
- 5 min read
Updated: Mar 29

ServiceNow Script Includes are a powerful way to organize and reuse server-side code. They allow developers to define functions or classes that can be invoked from business rules, client scripts, workflows, or even other script includes. A common question arises when working in a script include: how can one function call another function within the same script include? This guide explains the correct approach and best practices for calling a function from within its own script include.
Understanding Script Includes
In ServiceNow, script includes are server-side scripts used to store reusable JavaScript functions or classes. Each script include can define either an object-oriented class with methods or a global (classless) function. These scripts help keep your code modular and maintainable by centralizing logic in one place. When you create a new script include, the system auto-populates a template with a class structure (including an initialize function and a prototype block). This structure serves as a container for your methods, and it allows those methods to interact with each other through the script include instance.
Common Use Case: Calling a Function Within the Same Script Include
One frequent scenario is needing to call a helper function or subroutine from another function inside the same script include. The good news is yes, you can call a function within its own script include – but it must be done correctly to avoid scope issues or errors.
Example of Incorrect Usage
Consider the following script include code where one function tries to call another directly, without proper context:
var SampleScript = Class.create();
SampleScript.prototype = {
initialize: function() {},
functionA: function() {
functionB(5);
// Incorrect - functionB is not recognized here
},
functionB: function(num) {
gs.info('Number: ' + num);
},
type: 'SampleScript'
};
In this example, functionA attempts to call functionB(5) directly. This will not work and will result in an error because the script runtime does not recognize functionB in that scope. The call functionB(5) is treated as a call to a standalone function (which doesn’t exist in the global scope) rather than the method functionB of the current object. As experts note, if you are calling a method directly inside a script include, you need to prefix it with the current object context (e.g. this)
Correct Method: Using the this Keyword
To properly call another function within the same script include, use the this keyword to reference the current object instance.
Here’s the corrected version of the script include:
var SampleScript = Class.create();
SampleScript.prototype = {
initialize: function() {},
functionA: function() {
this.functionB(5);
// Correct - using 'this' to call internal method
},
functionB: function(num) {
gs.info('Number: ' + num);
},
type: 'SampleScript'
};
In this version, functionA calls this.functionB(5). The this keyword refers to the current instance of SampleScript, so this.functionB correctly resolves to the method defined in the same prototype. Using this ensures the script knows we are calling the internal function functionB on the current object. In other words, this.functionB(5) invokes the function functionB in the same script include instance, passing 5 as an argument. This approach will execute without errors, and you would see a log entry like “Number: 5” in the system logs as expected.
Best Practices for Calling Functions in Script Includes
When working with script includes and internal function calls, keep the following best practices in mind:
Use this to Reference Internal Methods: Always use this.methodName() when calling another function defined in the same script include. This tells the system to look for the function in the current object’s scope. Forgetting this will lead to the function being called out of context (or not found at all), as demonstrated above.
Encapsulate Business Logic Properly: Design your script include so that each function has a clear purpose, and the script include itself represents a cohesive set of related logic. Script includes help keep code organized; they promote modularity and maintainability by centralizing reusable logic. Avoid putting unrelated functions together, and consider splitting into multiple script includes if needed for clarity.
Avoid Global Variables: Do not declare variables in the script include’s outer scope (outside of any function) unless absolutely necessary. Variables declared outside any function in a script include become global, which can lead to conflicts or unexpected behavior if the same variable name is used elsewhere. Instead, use local variables inside functions or properties on this if you need to maintain state within the object. This ensures that your data is encapsulated within the script include instance.
Use Logging for Debugging: To troubleshoot issues within script includes, use logging statements like gs.info() or gs.info() at strategic points in your code. For example, you might log function entry and exit, or important variable values, to trace the flow of execution. Adding gs.info('SampleScript.functionA starting, input=' + someValue); can help verify that your internal function calls are happening as expected. These log messages will appear in System Logs > System Log > Application Log. Using such logs is a simple but effective way to debug – as one developer suggests, placing gs.info() lines in your script include helps you “know where the problem is” and verify that parameters are being passed correctly. Remember that in a scoped application, you should use gs.info() (or gs.debug()/gs.error()) because gs.info() is only for the global scope.
Alternative Approach: Using Static or Classless Script Includes
In some cases, a script include can be designed to have functions that are callable without needing to create an instance (object). This can be done by writing a classless script include (also known as an on-demand or static script include). For example, instead of using the Class.create/prototype structure, you could define a script include that simply contains a function with the same name as the script include. Such a script include might look like:
// Classless script include (function-based)
function UtilityFunctions() {
// This function name matches the Script Include name.
return {
sayHello: function(name) {
return "Hello, " + name;
}
};
}
If the script include is set to be client-callable (isPublic = true) or is global, you can call its functions without explicitly constructing an object. For instance, on the server side you might directly call UtilityFunctions().sayHello('John') to execute the function. More commonly, ServiceNow developers create static methods by extending certain classes or using the script include as a singleton. One typical scenario is extending AbstractAjaxProcessor for script includes that need to be called via GlideAjax from client scripts. In such cases, your script include methods (marked as public) can be invoked from client-side code without manually instantiating the class (the GlideAjax process handles it).
It’s worth noting that static or classless script includes are generally used for special situations (like global utility functions or AJAX entry points). For regular server-side use and for maintainability, the class-based approach (using this for internal calls) is recommended for clarity and consistency.
Conclusion
Calling a function from within the same script include is straightforward once you know the correct pattern. By using this to reference internal methods, you ensure that your script include’s functions can call each other reliably. We demonstrated how an incorrect direct call leads to errors and how the correct usage of this.functionName() resolves the issue. Adhering to best practices—such as keeping your code modular, avoiding unintended globals, and leveraging logging for debugging—will result in cleaner and more reliable script includes. With these techniques, you can confidently build script includes that have multiple internal functions working together, without running into scope problems.