How To Call a Script Include from a Business Rule - The Snowball (2024)

Table of Contents

Overview

Have you ever wanted to call a Script Include, directly from a Business Rule?

This is a common method to keep your business rules clean.

It’s especially useful if you plan on repeatedly calling the same block of code.

You can just define the Script Include one time, and you can reference it on the server as frequently as you’d like on the server.

Let’s review how to accomplish this.

How To Call A Script Include from a Business Rule

First, create the business rule – call it whatever you’d like. The below values are all fillers that aim to get the point across, and is not an actual use case for an IT Org as it’s an example. It doesn’t matter what the Business Rule is called, as long as it properly identifies what you’re trying to accomplish.

A Business Rule is a piece of JavaScript code that runs on the server, when the conditions defined on the business rule are met. These frequently run for CRUD operations in ServiceNow, for any table.

The example we will use is: Automatically create a Change Request, for every P1 Incident that is created. This will be accomplished by creating a business rule and then referencing a function that’s in a Script Include.

To simply call the script include and have it execute, there are just 2 lines of JavaScript involved – so there is really no complex code here. Although the below example has a little more to provide more context.

Let’s cover the business rule and then I’ll show you how to properly connect the 2 pieces of code.

The Business Rule

A business rule is just a piece of JavaScript code that runs when we tell it to. For this instance, we will have the code run whenever we create a P1 Incident.

The business rule details are outlined below.

When: Before

Insert: true

Condition: current.priority == 1;

Script:
(function executeRule(current, previous /*null when async*/ ) {

var number = current.number.toString();
var callIt = new scriptInclude();
callIt.createChg(number);

gs.addInfoMessage(“Automatically creating a Change Request”);

})(current, previous);

How To Call a Script Include from a Business Rule - The Snowball (1)

In the Business Rule, we have the following 2 lines of code – this is where we call the script include:

var callIt = new scriptInclude();
callIt.createChg(number);

Here “callIt” is just a variable that we are storing a newly defined object. When we use “new”, we are doing a few things – but most notably, we are creating an object, of the data type object.

Next, we use the newly defined object to call a function in our Script Include.

Remember, a Script Include is just a library of ServiceNow Classes and Functions.

So the syntax broken down is the following:

var anyVariableName = new scriptIncludeName();

anyVariableName.functionInScriptInclude();

NOTE: The name of the Script Include is important. You must define the exact name of the Script Include record in your business rule. This is where the 2 records are tied together and connected. If the name is not properly configured, the whole process will break.

We will create the Script include in the next step, to put it all together.

The Script Include

There are 2 types of script includes in ServiceNow.

A Classless Script Include and one that creates a class. Both are fine options and we’ll review each below.

1) The Classless Script Include

How To Call a Script Include from a Business Rule - The Snowball (2)

A classless Script Include, is when you delete all of the default boiler plate script that comes whenever a new script include is created. ServiceNow wants you to create a new class, out of box. But you really don’t have to. You can remove all of the code and just define your function. This is a fine option if you just want to create your own function and call it.

Keep in mind that when you call a classless script include, you actually don’t even need to add ‘new’ when you’re in the business rule.

This is a simpler option – but the choice is yours.

So you could just do the following in your business rule, to call a function in a script include:

var anyVariableName = newscriptIncludeName().functionInScriptInclude();

2) Creating A Class

How To Call a Script Include from a Business Rule - The Snowball (3)

When you create a new Script Include, ServiceNow adds some default code to start building out your JavaScript class, as seen above. So you are actually creating your own JavaScript class.

I went ahead and kept the code, but you can actually delete the entire default class code and add the function as the only code in the Script field. This will work perfectly fine.

Script:

var scriptInclude = Class.create();
scriptInclude.prototype = {
initialize: function() {
},

createChg: function(number) {

var chg = new GlideRecord(“change_request”);
chg.initialize();
chg.short_description = “Created from Incident: ” + number;
chg.type = ‘normal’;

/*
Fill out other field values like below if you’d like

Format:
chg.field = value;
*/

chg.insert();

},

type: ‘scriptInclude’
};

How To Call a Script Include from a Business Rule - The Snowball (4)

Why Would I Want To Call a Script Include?

There are several reasons why this is beneficial.

1) Keeps Your Code Clean

When you’re writing really long business rules, you should consider breaking the functions down into smaller pieces and putting them into a Script Include.

When writing functions, try to aim for one function to perform one action – that’s it. I like to have a lot of little functions, I feel this keeps code really clean and allows for future developers to come in and easily understand what is going on.

When referencing a script include in a business rule, it’s easy for a ServiceNow Developer to see it and understand what is happening. It really improves the readability of code.

2) Allows Easy Repeat Use – Reuse Code

When you have script includes clearly defined – they become dynamic and “reusable”.

Say for example you have a Jira Integraion with ServiceNow, and at several different lifecycles of your task based applications, you want to sent a ServiceNow task to Jira. You may want to send Incidents, Problems, Changes and even custom task based records to Jira. To easily accomplish this, you would write your code once in a script include, and then reference it in different business rules across the system.

Why would you want to have the same 50+ lines of code, cut/paste several times throughout your system? Say something needs to get updated as well, you just need to update your script a single time, and then all of the business rules that call it will be automatically referencing the updated script.

Script includes allow a ServiceNow Environment to properly scale from a developers perspective and makes our lives easier.

3) Helps Out Your Team

If you’re sharing a ServiceNow environment with other admins or developers – you’re helping the team by putting your server side code in a script include.

You are allowing others to more easily access and use your code.

Say for example that your organization has a Slack integration. If this server side code to execute and control this is in one business rule, then you’ll only be able to use it under limited circ*mstances.

But if this code is in a script include, it can be called globally from across the sytem. You and the other ServiceNow Developers can easily call and reference the code. No one needs to rewrite the integration.

Enable the other members on your team, and even show them how to reference your script includes. They’ll thank you.

How To Test The Script

Putting it all together here, we will create a P1 Incident, which will allow our business rule to run, where in the business rule, we are executing a function in a Script Include that we created.

Or for a more visual shorthand version:

Incident created > Business Rule > Script Include

How To Call a Script Include from a Business Rule - The Snowball (5)

As we can now see, we have a new Change Request record that has been created.

The Change request has data that we captured in our Business Rule and passed into our Script Include function.

How To Call a Script Include from a Business Rule - The Snowball (6)

What are some other use cases that you’ve come across when doing this?

Are there other ways to accomplish the same goal?

Let us know in the comments.

How To Call a Script Include from a Business Rule - The Snowball (2024)

FAQs

How do you call a script include from a business rule? ›

In the Business Rule, we have the following 2 lines of code – this is where we call the script include: var callIt = new scriptInclude(); callIt. createChg(number);

How do you call one script include from another script include? ›

Hello Priya,
  1. You can call script include from another script include as below: var osi = new otherScriptInclude(); ...
  2. Way to call script include from Business rule, workflow activity or or from server side script: ...
  3. Way to call script include from client script, Catalog Client Script or UI policies.
Jul 22, 2019

Can we call client callable script include from business rule? ›

It Is actually a reusable script logic which will only execute when called by other scripts such as business rule, script action, client script in servicenow (through glide ajax) etc. We can call script include function and classes anywhere while doing server-side scripting in ServiceNow.

Where can a script include function be called from? ›

The script include usually comprises a set of JavaScript functions that can be called from any server-side scripts. Moreover, it is possible to mark a script include to be client callable, thereby making it possible for client-side scripts to make remote (AJAX) calls to script include code.

How do you write a script call? ›

Example of a Cold Calling Script

This is [name] from [company]. (pause) Do you have two minutes to chat? (prospect says yes) Great, thank you. We're a [describe company] platform that helps companies like yours [problem you solve]. I'm calling to see if we can provide assistance.

How do you write a script rule? ›

15 Simple Screenplay Rules You Need to Know
  1. Keep Title Pages Simple. ...
  2. Only Use FADE IN and FADE OUT at the Beginning and End of Your Script. ...
  3. Slug Lines — a.k.a. Location Headings — Should Only Have Three Pieces of Information. ...
  4. Little to No Camera Directions. ...
  5. Dialogue Never Follows the Slug Line.
Jan 17, 2021

How do you reference one script to another? ›

Adding a script reference
  1. Create a single script that you want to re-use or reference from another script.
  2. Create a new driving script that will call your re-used script.
  3. Expand the Properties tab.
  4. Right click and select Add . NET Script Reference.
  5. Select the script that you want to use click OK.

Can we call script include from client script if yes then how? ›

We can call script include from client script using Glideajax. Calling a script include from the business rule or from any server-side scripting it is very easy and straightforward syntax is there for that.

How do you call one function of script include to another function of same script include? ›

use this. functionname() for calling functions of the same script ... use this. functionname() for calling functions of the same script ...

How do you call a client script from business rule in ServiceNow? ›

You cannot call business rule through client script. You can only get the scratchpad value through display business rule. If you want to call server side script through client side you need to write script include and then call it through GlideAjax.

How do you answer a customer service call script? ›

Great, thank you. Have a nice day. Thanks for using the 24-hour help service, and please feel free to reach out to us if you need anything else. I'm so glad we could help and that you found exactly what you were looking for.

How do you call a client callable script from the server side? ›

You can call Server side script include from a client callable script include using gs. include("ScriptInclude").

What is script includes and why do we use script include? ›

Script Includes defines a function or class to be reusable server-side script logic that execute only when explicitly called by other scripts. Let us talk about Script Includes later someday (You still can read about them at Docs or Here). Today we will be creating a Utilities Script Include to help you code smart.

How to pass multiple values from script include to client script? ›

If you want a function a Script Include to return multiple values you should return an object instead of one value. This object should be converted to a JSON string. This way you can retrieve it in your Client Script.

How do you call a function in script tag? ›

The simplest way to call a JavaScript function in an HTML document is to define the function inside a pair of <script> tags, then place it inside either the head or body of the HTML document. You can then call this function when a user interacts with a page element.

How do you start a call script? ›

Hello, [PROSPECT'S NAME]. This is [YOUR NAME] from [COMPANY]. How are you today? I wanted to give you a quick call because we're working with a few companies similar to yours who are looking to [SOLUTION OFFERING], and that's what we do.

How do I start a cold call script? ›

17 good opening lines for cold calling
  1. 1 - Introduce yourself. ...
  2. 2 - Be upfront. ...
  3. 3 - Ask how they are. ...
  4. 4 - Give a reason for the call. ...
  5. 5 - Emphasise the importance of the call. ...
  6. 6 - Point out that you're happy they picked up. ...
  7. 7 - Ask for help. ...
  8. 8 - Thank them for their time.
Aug 19, 2022

How do you start a sales call script? ›

Sales Script Template:

“Hi, Mr./Mrs./Miss [client's surname]. My name is [agent's name], and I am calling from [company's name]. I am reaching out to you because I might have a great solution to your current business needs and I'd love to talk through it with you. Please call me back when it's a good time for you.

What are the 4 things that a script must have? ›

There are only four elements you can use to tell a screen story: images, action, sound effects, and dialogue.

What are the 5 elements of a script? ›

The 5 elements that make up a great story

For this introduction, we're going to call them character, want and need, plot, structure, and conflict and resolution.

What are the 3 C's of writing a good script? ›

So we have concept, characters, and conflict — the Three Cs.

How do you repeat the same reference? ›

The abbreviation ibid (meaning 'in the same place') can be used to repeat a citation in the immediately preceding footnote.

How do you reference the same source twice? ›

If the subsequent citation is in the footnote immediately following the full citation, you can use 'ibid'. Used alone, 'ibid' means 'in the very same place' – in other words, the same source and the same page or paragraph as the preceding full citation.

How do you write a reference from another source? ›

Include information in the following order:
  1. author's surname and initial.
  2. year of publication.
  3. name of article (between single quotation marks with minimal capitalisation)
  4. in.
  5. initial(s) and surname(s) of editor(s)
  6. (ed.) or (eds)
  7. name of collection (the name on the title page) in italics and minimal capitalisation.
Jun 27, 2022

Can we call script include from UI action? ›

Script include can be called in UI Action using both Client and Server types.

Can we call script include from UI policy? ›

You should be able to call your script include in the script section of the UI Policy, just check the advanced box. UI Policy script section is like client script.

What is the difference between client script and script include? ›

For ex, Client script we can use to alert something on load of form. In short, we can only use client script for the manipulations that we can see on our browser. But, Script include use to work for back end database operations which are not possible using client script.

How do you call a function with parameters from another function in JavaScript? ›

The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

How do you call a shell script function from another shell script? ›

this: source /path/to/script; Or use a bash command to execute it: /bin/bash /path/to/script; The 1st and 3rd methods execute a script as another process, so variables and functions in another script will not be accessible.

What are different types of script include and how will you call a script include in business rule? ›

Script Includes are reusable server-side script logic that define a function or class. Script Includes execute their script logic only when explicitly called by other scripts. There are different types of Script Includes: On demand/classless.

How do you trigger an event from business rule ServiceNow? ›

Using a business rule to trigger events
  1. In Studio, open the Create New Application File wizard, select Server Development | Business Rule, and click on the Create button to open the new Business Rule record form.
  2. Populate the form with the following values: Name: Monitor changes to assigned to field.

Which runs first business rule or client script in ServiceNow? ›

"Client-based code that executes in the browser, using Ajax or running as Javascript, always executes before the form submission to the server." This includes Client scripts and UI Policies.

How do you call a notification from a business rule in ServiceNow? ›

You can do this in the notifications. Go to System Notifications -> Email -> Notifications and click on New to create a new notification. Refer to below screenshot. Once this is created, then you are all set.

How will you describe the proper way of answering a call? ›

Answering Calls
  1. Try to answer the phone within three rings. ...
  2. Answer with a friendly greeting. ...
  3. Smile - it shows, even through the phone lines; speak in a pleasant tone of voice - the caller will appreciate it.
  4. Ask the caller for their name, even if their name is not necessary for the call.
Jul 7, 2020

How to handle irate customer sample script? ›

I'm sad to hear about your negative experience with us. Please tell me what happened and I'll do everything I can to make things right. I understand how you're feeling right now, and I'm very sorry. I'm sending your request to the right person immediately to make sure we correct this as soon as possible.

How do you explain a call and response? ›

In music, call and response is a technique where one musician offers a phrase and a second player answers with a direct commentary or response to the offered phrase. The musicians build on each other's offering and work together to move the song along and create a sound that's inventive and collective.

What is client callable in script include? ›

Client callable script includes in ServiceNow are typically combined with an ServiceNow API method called GlideAjax. It's a method that handles the actual AJAX part of your script that sends and receives data between the client and the server.

What is server-side scripting List 3 examples of server-side scripts? ›

Server-side scripting languages
  • Net.Data. Net. ...
  • Node.js. Node. ...
  • PHP. Hypertext Preprocessor (PHP) is one of the world's most popular server-side scripting language for building dynamic, data-driven Web applications.
  • Python.

What is difference between client-side scripting and server-side scripting? ›

Server-side scripting is utilized in the backend when the source code is invisible or hidden on the client side. In contrast, client-side scripting is utilized at the front end, which users may access via the browser. The files are inaccessible to the client-side script.

What are 4 types of scripts? ›

Here are eight types of scripts that you can write:
  • Original script. Original scripts include those that you create from your own ideas. ...
  • Adapted script. An adapted script re-imagines an existing story or narrative. ...
  • Screenplay. ...
  • Storyboard. ...
  • Spec script. ...
  • Standalone script. ...
  • Pitch script. ...
  • Shooting script.
Mar 11, 2022

How do you pass a value in a script? ›

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

How script include is better than business rule in Servicenow? ›

Consider using script includes instead of global business rules because script includes are only loaded on request. Script include formScript includes have a name, description and script. They also specify whether they are active or not, and whether they can be called from a client script.

How do I pass multiple parameters to a PowerShell script? ›

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

What do you mean by call () apply () and bind () methods? ›

The call, bind and apply methods can be used to set the this keyword independent of how a function is called. The bind method creates a copy of the function and sets the this keyword, while the call and apply methods sets the this keyword and calls the function immediately.

How do you write a function and call function in shell script? ›

Creating Functions

The name of your function is function_name, and that's what you will use to call it from elsewhere in your scripts. The function name must be followed by parentheses, followed by a list of commands enclosed within braces.

How do you write a function and call it? ›

How do I call a function?
  1. Write the name of the function.
  2. Add parentheses () after the function's name.
  3. Inside the parenthesis, add any parameters that the function requires, separated by commas.
  4. End the line with a semicolon ; .

What is the difference between business rule and script include? ›

Create script includes to store JavaScript functions and classes for use by server scripts. Each script include defines either an object class or a function. Consider using script includes instead of global business rules because script includes are only loaded on request.

What are the types of script includes? ›

There are different types of Script Includes:
  • On demand/classless.
  • Extend an existing class.
  • Define a new class.
Apr 28, 2019

What is a business script? ›

A sales script is a set of guidelines that can help you keep your message consistent across your company and improve your sales techniques. A sales script is an effective way to engage potential customers in a consistent manner across your company.

What are the three acts in a script? ›

Act I is the setup or exposition. This establishes the main characters and their goals. Act II raises the stakes culminating in the confrontation between the hero and the villain. Act III resolves the story.

What is the most common script? ›

The Latin alphabet is the most widely used script, with nearly 70 percent of the world's population employing it. It commonly consists of 26 letters and is the basis for the International Phonetic Alphabet, which is used to relate the phonetics of all languages.

What is a business rule example? ›

Business rules are often used to provide customer discounts. For example, a brand can award customer loyalty by setting up a discount policy that offers a 15% discount when a customer spends $1000 in a month on purchases. A staff member can set business rules that offer discounts after a certain value has been reached.

What are the 3 rules of business? ›

They discovered that the super successful companies used three doctrinal rules. Better before cheaper. Revenue before cost. There are no other rules.

What are the three types of business rules to control a business process? ›

This can be further broken down into three different subsets of rules, which include stimulus and response, operation constraints, and structure constraints.

What are the four elements of a script? ›

There are only four elements you can use to tell a screen story: images, action, sound effects, and dialogue.

What are the 6 aspects of a script? ›

The basic format consists of six major elements: scene headings, action, character name, dialogue, parentheticals, and transitions.

What are the two types of scripts? ›

  • Commissioned screenplays are those that are written by authors that producers hire for a specific show or franchise.
  • Spec, or speculative, script is an unsolicited screenplay that you write in hopes of selling.
  • There is a difference between composing a screenplay for a movie and a TV show.

What are scripts example? ›

A script or scripting language is a computer language with several commands within a file capable of being executed without being compiled. Examples of server-side scripting languages include Perl, PHP, and Python. The best example of a client side scripting language is JavaScript.

What does calling a script mean? ›

A call script, a written script entailing correct wording and logic aids, assists an agent in handling a contact. It also assists in the maintenance of focusing on the content of the contact.

What is an example of a sales call script? ›

“Hi, Mr./Mrs./Miss [client's surname]. My name is [agent's name], and I am calling from [company's name]. I am reaching out to you because I might have a great solution to your current business needs and I'd love to talk through it with you. Please call me back when it's a good time for you.

Top Articles
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated:

Views: 5585

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.