Skip to tutorial content
Rate module Not yet rated

Calling Script Includes

2 min read
On this page

In this lesson, you will learn:

  • How to call a class-style script include
  • Where you can call script includes from
  • How to call one from another scope

1 The calling pattern

To use a class-style script include, you create a new copy with new, then call a method with a dot. πŸ“ž

var utils = new SNWRIncidentUtils();
var count = utils.getOpenCount(current.caller_id);

// or in one line:
var count2 = new SNWRIncidentUtils().getOpenCount(current.caller_id);

A single-function script include is called like any function: snwrIsWeekend(current.opened_at).

How a call to a script include works. The caller writes new SNWRIncidentUtils().getOpenCount(current.caller_id). Step 1: ServiceNow finds the script include named SNWRIncidentUtils. Step 2: it creates a new object and runs initialize. Step 3: it runs getOpenCount with Riya's sys_id. Step 4: it returns the number 3 to the caller.
Figure 1. new, then the method name, then use the result.

2 Where you can call it from

Place Example
Business rule var n = new SNWRIncidentUtils().getOpenCount(current.caller_id);
UI action condition new SNWRIncidentUtils().getOpenCount(current.caller_id) < 5
Reference qualifier javascript:new SNWRRefQuals().activeInSameDept(current)
Default value javascript:new SNWRDefaults().defaultGroup()
Scheduled job, flow script, Scripts – Background Same pattern: new ...().method()

πŸ“ Note: Client scripts can’t call a script include directly, because it runs on the server. They use GlideAjax (Lesson 40.3).

3 Calling across scopes

A script include in a scoped app has a prefix in its API Name, like x_snwr_rooms.RoomUtils. From another scope, use the full API Name:

var free = new x_snwr_rooms.RoomUtils().isRoomFree('Room 301');

⚠️ Heads up: This only works if Accessible from is set to All application scopes. Otherwise the call fails. (More on scopes in Chapter 46.)

⭐ Remember: new ClassName().method(args). Other scopes need the full API Name.

4 Let’s try it: use your toolbox

  1. Open Scripts – Background.
  2. Run: gs.info(new SNWRIncidentUtils().getOpenCount(gs.getUserID())); How many open incidents are you the caller on?
  3. Now use it in a business rule: before insert on Incident, and if the caller has 5 or more open incidents, add a message “This caller has many open incidents.”
  4. Test it by creating an incident for a busy caller.

5 Quick recap

  • Call a class with new ClassName().method()
  • Call a function-style include like a normal function
  • Use them in business rules, conditions, qualifiers, defaults, jobs, and more
  • Across scopes, use the full API Name and set Accessible from to All application scopes

6 Check yourself

Think of your answer first. Then tap to check.

1 Write the call to method getVipCount on class SNWRUserUtils. Show answer

new SNWRUserUtils().getVipCount()

2 Can a client script call a script include directly? Show answer

No. It must use GlideAjax.

3 A scoped script include can’t be called from global. What should you check? Show answer

That Accessible from is All application scopes, and that you’re using the full API Name.

Nice work! You finished this lesson. πŸŽ‰

Was this lesson helpful?

Have a question or doubt?

Ask here and get help from instructors and fellow learners.

0 / 300

Only logged-in learners can post.