- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
3 weeks ago
Good code is easy to read, reuse, and maintain. In a ServiceNow environment, that starts with a clear separation of concerns. Instead of scattering logic across many places, bundle behavior into reusable modules. On ServiceNow, those modules are Script Includes. This guide walks through creating a Script Include in the Zurich release, wiring it into Scripts - Background, and applying best practices you can rely on across a production-grade app. It is part of the javascript full course series, built with the latest ServiceNow Studio and a Personal Developer Instance.
Why Separation of Concerns Matters in ServiceNow
Separation of concerns keeps logic organized. Each part of your system handles one responsibility. In practice, it means:
- Reusable modules: Shared utilities live in one place, not copied everywhere.
- Cleaner workflows: UI scripts call functions, Script Includes do the heavy lifting.
- Easier maintenance: Fix a bug in one class, and the fix applies everywhere.
- Safer upgrades: You avoid breaking unrelated pieces when changing one feature.
In ServiceNow, that module is a Script Include. It is a server-side class you can call from other scripts, UI Actions, Business Rules, and even across application scopes if allowed.
Working in Zurich with the Latest Studio
This walkthrough uses a Personal Developer Instance upgraded from Yokohama to Zurich. Zurich and the latest ServiceNow Studio give a better developer experience, faster search, and modern JavaScript support in scoped apps.
- PDI: Personal Developer Instance for hands-on building and testing
- Version: Upgraded to Zurich, the latest at the time of recording
- Tools: Studio under System Applications, the go-to IDE for pro-code work
If you have not set up a PDI yet or have not upgraded, do that first so your features match what you see here. Studio in Zurich streamlines creation, search, and script editing, so keeping current pays off.
Create a Scoped Application (the Foundation for Clean Code)
Scopes keep your app’s objects organized. In ServiceNow, a scope is like a folder that holds all your app artifacts, including scripts, tables, and UI elements. It also prevents naming collisions with other apps.
High-level steps:
- Open Start Building in the top right of your instance.
- Launch Studio under System Applications.
- Create a new application. Use a clear name and a short description, for example: “Separation of concerns demo.”
- Accept the default roles Studio suggests, then add an admin-style role for your app if needed, for example:
demoadmin. - Stay inside your app’s scope while building. Platform apps often start with SN. Custom app scopes start with X, which helps avoid conflicts.
Why this matters: when you add Script Includes or any server-side logic, your scope controls how it is named, who can call it, and how it is packaged.
Script Includes: Your Reusable Server-Side Classes
A Script Include is a server-side JavaScript class. It exposes functions you can call from Business Rules, UI Actions, Scripted REST APIs, and Scripts - Background. Most teams use Script Includes to hold shared logic, such as validation, formatting, lookups, and integrations.
Key properties to set when creating a Script Include:
- Name: A clear, class-style name, for example: “HelloWorld.”
- API name: Studio generates this from your app scope and class name.
- Accessible from: Choose whether other apps can call it. For a utility class, allow it to be called from all application scopes.
- Client callable: Keep this off unless you intend to call it from client-side code.
Inside the Script Include, you will see a pattern like this:
- A constructor function named
initialize. It runs when the object is created. - Methods that perform your work. For example,
sayHello(name)could return a greeting string.
Best practices:
- Comment your functions. Short, clear notes help a lot when you come back months later.
- Keep it focused. One Script Include for one responsibility, not a grab bag of unrelated helpers.
- Name methods clearly. Use verbs that describe the action.
Example Walkthrough: A Simple HelloWorld Script Include
In Studio, create a Script Include called “HelloWorld” and save it. Add two simple functions. One can return a greeting using a name argument. Another can demonstrate a different behavior so you can practice calling multiple methods. The class becomes callable anywhere in your scope once you save it.
What this shows:
- A class can expose multiple functions.
- Each function handles one small task.
- Your calling scripts stay clean, since they only call a method and output the result.
If your Script Include needs to log messages, use the global system object from the server side. A common pattern is to return a value from your Script Include method, then print it using gs.info in the caller. That keeps the class testable and keeps display logic outside your class.
Run and Test from Scripts - Background
Scripts - Background is your lab. It is ideal for quick tests, learning new patterns, or validating a Script Include without wiring up UI pieces.
Steps to call your Script Include:
- Switch to your app scope in the lower left of the platform UI, so the code runs with ES6 support and the right permissions.
- Create a new instance of your class using the
newkeyword. For example, create a variable and assignnew <APIName>(). - Call your method with a parameter, for example a user name like “Clark Kent.”
- Capture the return value and print it using
gs.info.
Two important tips from the session:
- If you hit an execution error when using
let, check your scope. In Zurich, scoped apps support ES6 features likelet. Global scope may behave differently. - If you get a “not defined” style error, copy the API name directly from the Script Include record. Do not guess it.
This quick loop makes it easy to test logic before you wire it into Business Rules, Flow Designer actions, or any server-side script.
What “new” Really Does Here
When you call new with your Script Include class, the platform creates an instance of that class. Think of your computer’s RAM as a big box. Each user action gets a small, dedicated slot in that box for its instance. If a thousand users call the same class, each one gets a separate instance. Their data does not collide. That keeps processing safe and predictable under load.
This mental model helps when you scale. You can design classes to be stateless and pass in all needed inputs. That makes your logic easier to test and safer to reuse.
Save Early, Format Often, Document What Matters
Studio in Zurich adds quality-of-life features that speed up development:
- Save from the tab header, and save often.
- Use the format button to clean up indentation. Readable code makes reviewing faster.
- Write short comments at the top of your functions. A single line like “Says hello to the given name” is enough.
These small habits add up when your app grows to hundreds or thousands of lines across many files.
Access Across Scopes, Done Right
You can make a Script Include available to other apps by setting the “accessible from” option to all application scopes. Use this for utility classes that multiple apps can consume. Keep cross-scope access minimal and intentional. For private, app-only logic, keep it scoped to the app.
This control supports good boundaries. Other teams can use your approved APIs without reaching into your internals.
Common Missteps and How to Fix Them
- Wrong API name: Always copy the API name from the Script Include. Paste it into Scripts - Background to avoid typos.
- Using
letin the wrong scope: If you see odd behavior, run your code inside your scoped app while on Zurich. Scoped scripts support modern JavaScript better than legacy global code. - Mixing display and logic: Return data from your Script Include method. Print with
gs.infoin the caller. Keep concerns separate. - Not saving before testing: Save in Studio, then run from Scripts - Background. If you forget to save, the old version runs.
Putting It Together With a Simple Flow
- Create PDI, upgrade to Zurich, and open Studio.
- Create a new scoped application with a short description and an admin-style role.
- Add a Script Include named “HelloWorld,” accessible from all application scopes if you need reuse across apps.
- Write a small
initializeconstructor and at least one method that returns a string. - Save and format.
- Switch to your app scope, open Scripts - Background, instantiate the class with
new, call the method, and log it. - Fix any errors by checking the API name and scope, then rerun.
This pattern works for more than greetings. Use it for validations, record fetchers, calculations, approval helpers, and integration wrappers.
Why This Fits a Professional Workflow
Adopting Script Includes for core logic lets you keep UI rules, flows, and client scripts light. Each caller asks for what it needs and prints or displays the result. You gain:
- Consistency across features
- Testability at the function level
- Scalability under load
- Reusability across scoped apps
That is the backbone of clean ServiceNow development, especially on Zurich using the latest Studio.
Keep Learning With the Zurich Series
This tutorial is part of a larger javascript full course built on Zurich. New lessons use the same PDI and the latest Studio, so your setup will match the demos:
- Setting up and upgrading your PDI to Zurich
- Organizing code by scope and role
- Writing Script Includes for reusable server logic
- Executing and debugging in Scripts - Background
- Moving from simple helpers to full service classes
To go deeper, you can follow my JavaScript youtube playlist to firm our coding skills and understanding.
found here:
If you want to support more tutorials and access members-only resources, join the channel through the membership page with exclusive resources and early access.
Conclusion
Separation of concerns turns scattered scripts into a clear system. Script Includes on Zurich give you reusable server-side classes that keep logic clean, testable, and easy to scale. Start with a scoped app, add a focused Script Include, test from Scripts - Background, then reuse it across features. Ready for more? Watch the full course playlist on the channel, and check back each week for new Zurich-based lessons that build your skills step by step.
- 202 Views
