What It Does

The gs.include() method searches for a Script Include record with the specified name and loads its script content into the current execution context. This makes any functions or variables defined in that Script Include immediately available to the calling script without requiring explicit instantiation.

Internally, ServiceNow locates the Script Include record in the sys_script_include table where the Name field matches the provided parameter. If found, the platform evaluates the script content in the current scope, effectively executing all top-level code within that Script Include. This differs from modern Script Include instantiation where you create an object instance.

The method returns true when the Script Include is successfully located and loaded, false when the Script Include name cannot be found or when a runtime error occurs during script evaluation. The platform does not throw exceptions for missing Script Includes, making error handling dependent on checking the return value.

⚠️

gs.include() only works in global scope. Scoped applications cannot use this method to load Script Includes from other scopes.

Edge cases include attempting to load Script Includes that contain syntax errors, which causes gs.include() to return false and potentially log JavaScript errors. Loading the same Script Include multiple times will re-execute all initialization code, which can cause unexpected behavior if the Script Include modifies global variables or performs side effects during load.

This method predates modern Script Include patterns and relates to gs.evaluate() in that both execute code in the current context, but gs.include() specifically targets Script Include records while gs.evaluate() executes arbitrary JavaScript strings.

When to Use This

Use gs.include() when working with legacy global Script Includes that define utility functions without using the class-based pattern. This is most common in older ServiceNow instances or when maintaining existing code that relies on function-based Script Includes rather than instantiable classes.

For modern development, use direct instantiation with new ScriptIncludeName() instead. This approach provides better performance, clearer dependencies, and works in both global and scoped applications. Avoid using gs.include() for loading Script Includes that follow the class pattern with initialize functions.

Common misuse includes attempting to use this method to conditionally load Script Includes for performance reasons, but the overhead of checking and loading at runtime often exceeds the memory savings. Another anti-pattern involves using gs.include() inside loops or frequently-called functions where the repeated loading degrades performance.

Return Value

Returns a boolean value: true when the Script Include exists and loads successfully, false when the Script Include cannot be found by name, is inactive, or encounters a JavaScript error during evaluation. The return value does not indicate whether the loaded functions work correctly, only whether the loading process completed.

Always check the return value before attempting to use functions from the loaded Script Include. Calling functions that failed to load will result in ReferenceError exceptions. Use the pattern if (gs.include('ScriptName')) { /* use functions */ } to handle loading failures gracefully.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime

Platform Behavior & Side Effects

  • Executes all top-level code in the Script Include immediately upon loading, including variable declarations and function calls outside of function definitions
  • Queries the sys_script_include table each time called, with no caching of the lookup results or loaded code
  • Does not trigger Business Rules, ACLs, or notifications since it reads the Script Include record directly using system-level access
  • Performance degrades with repeated calls as each invocation re-reads and re-evaluates the Script Include code
  • JavaScript errors during Script Include evaluation are logged but do not terminate the calling script execution
  • Only works in server-side contexts and fails silently in client-side scripts rather than throwing an error
  • Respects Script Include's Active field - inactive Script Includes cannot be loaded and return false