Class-based Script Includes create true object instances with state, unlike function-based ones that are just namespaces. The Class.create() pattern gives you constructor logic, private instance variables, and the ability to maintain state between method calls. The type property isn't decorative β€” it's required for proper instantiation, especially in scoped applications where the platform needs to resolve the class name correctly.

When to use this pattern

  • When you need to maintain configuration or state between method calls on the same instance
  • When building utilities that other developers will instantiate with new ClassName() rather than static method calls
  • When you need constructor parameters to customize behavior per instance
  • When building APIs that other scoped apps will consume β€” the type property ensures proper cross-scope instantiation

When NOT to use this pattern

  • For simple utility functions with no state β€” use function-based Script Includes instead for better performance
  • In client scripts β€” you need Client callable checked and GlideAjax pattern instead
  • When you only need static helper methods β€” creating instances adds unnecessary overhead
  • For one-off logic that won't be reused β€” put it directly in the Business Rule or calling script

Key behaviors and gotchas

  • The type property must exactly match the Script Include name β€” case sensitive
  • Missing initialize function causes silent failures when instantiating with new operator
  • Instance variables set in initialize with this.property persist across method calls on the same instance
  • Methods defined on prototype are shared across all instances β€” don't store instance-specific data there
  • Private methods prefixed with underscore are convention only β€” they're still accessible externally
  • Cross-scope instantiation requires the Script Include to be in a dependency relationship or global scope
⚠️

Forgetting the type property will cause 'ReferenceError: [ClassName] is not defined' when other scoped apps try to instantiate your class, even if the Script Include is accessible.

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