0% found this document useful (0 votes)
9 views

Script Include Use Case

Uploaded by

saik40616
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Script Include Use Case

Uploaded by

saik40616
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Tech tactics

Simple tactics that change the life of


your code

Hazim khasawneh

Sharing ideas and tips that help a front-end


developer in his daily life.
Weekly Posting Schedule

Join me every Sunday at 11 AM (Jordan time)

for a new document filled with valuable insights!


Script Includes in ServiceNow:

Script Includes are reusable server-side scripts that can be invoked from
other server-side scripts like Business Rules, Scheduled Jobs, or from the
client-side.
Why Use Script Includes?

✓ Reusability: Centralizes common code into one place, making it easier to


maintain and update.

✓ Modularity: Breaks down complex functionality into manageable, reusable


components.

✓ Performance: Improves performance by reducing redundant code execution.

✓ Maintainability: Easier to debug and modify when all logic is centralized.


Benefits of Using Script Includes :-

Reduced Code Duplication: Write once, reuse multiple times.

Simplified Development: Makes code cleaner and more organized.

Enhanced Collaboration: Easier for teams to work on different parts of the


application without conflict.

Easier Upgrades: Easier to update functionality without changing multiple places


in the code.
Creating Script Include
1. Navigate to All > system definition >
script includes.

Client callable: Select this option if client-


side scripts can call the Script Include using
GlideAjax.

Accessible from: Choose This application


scope only or All application scopes. Specifies
the scope(s) that can use the Script Include.
Using script include in
Business rule
1. A function named getYearlySalary() is
declared within a Script Include.

This function retrieves the salary input from


the user and calculates the annual salary
based on logical calculations.

You can open this widows once you download SN Utils </>
The function then returns the calculated
annual salary.
Using script include in
Business rule

1. A Business Rule named Calculate


Yearly Salary is created.

2. This Business Rule will run after record


inserted to system.

3. This Business Rule utilizes the


getYearlySalary() function from the
Script Include.

4. Advanced code in the next slide.


Using script include in
Business rule

1. We create a new instance of the


unifindFunctionsSources Script
Include.

2. The script calls the getYearlySalary


method from the Script Include using
the user-provided salary
(current.u_your_salary).

3. The calculated yearly salary is then


assigned to the u_yearly_salary field on
the current record.
Using script include in
Business rule

As shown in the image, after the record is


submitted, the Business Rule is triggered. It
then calls the getYearlySalary function from
the Script Include, which calculates the
yearly salary. The calculated value is then
returned and stored in the yearly salary field
on the form.
Using script include in
Client-Script

1. We have added new function called


getGreeting().

This function retrieves the current user's


name and generates a greeting message.

You can open this widows once you download SN Utils </>
Using script include in
Client-Script
1. A Client script named Greeting Message is
created.

2. This script runs on the onLoad event,


triggering each time the form is opened or
reloaded.

3. It uses the getGreeting() function from the


Script Include to generate a personalized
message.

4. GlideAjax is used to make asynchronous


server-side calls from the client-side in
ServiceNow. It allows the Client Script to
interact with the Script Include by passing
parameters and receiving data without
reloading the page.
Using script include in
Client-Script

Now once I open incident form I'll get this


message showing my name and kind words
come from script include , utilize it from
client script to show it to user.
Using script include in
Custom Widget

Last week, we created a custom widget for


rating resolved incidents, and the logic was
implemented in the server script section. We
will now refactor this code to enhance its
maintainability and sophistication. This will
be achieved by defining the same
functionality in a Script Include and then
calling that Script Include from the server
side of the custom widget. This approach
ensures that the functionality continues to
operate as expected while being more
professional and easier to maintain.
Using script include in
Custom Widget

1. We invoke the Script Include from the


server side in the custom widget.

2. We call the getResolvedIncidents()


function to retrieve resolved incidents
and store the results in an array.

3. We utilize the updateIncidentRating()


function to update the values provided
by the user and apply these updates to
the table.
In summary, using a Script Include simplifies development by providing a
unified location for script code that can be accessed from various contexts,
such as business rules, client scripts, custom widgets, and more. This approach
adheres to best practices by reducing code duplication and promoting reuse.
As a result, maintenance becomes significantly easier, as a single function can
be utilized multiple times across different components.
At The End.

Let's build a community where we share knowledge and simplify


tech. Simple insights can save time and effort for others in our
community.

Follow, comment , and repost useful content!


Thank you.

You might also like