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

6.script Include& Glide API's

The document discusses Glide APIs and Glide API methods in ServiceNow. Some key points: - Glide APIs allow developers to access and manipulate ServiceNow platform data, manage workflows, customize the UI, and integrate with external systems. - Common Glide API methods include GlideRecord for accessing records, GlideSystem for system operations, and GlideForm for interacting with forms. - Example methods described are addActiveQuery(), addEncodedQuery(), addQuery(), getRowCount(), getValue(), getDisplayValue(), next(), orderBy(), query(), get(), setAbortAction(), setDisplayValue(), setLimit(), and setValue(). These methods provide functionality for querying, filtering, sorting,

Uploaded by

Prem Kumar Vula
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

6.script Include& Glide API's

The document discusses Glide APIs and Glide API methods in ServiceNow. Some key points: - Glide APIs allow developers to access and manipulate ServiceNow platform data, manage workflows, customize the UI, and integrate with external systems. - Common Glide API methods include GlideRecord for accessing records, GlideSystem for system operations, and GlideForm for interacting with forms. - Example methods described are addActiveQuery(), addEncodedQuery(), addQuery(), getRowCount(), getValue(), getDisplayValue(), next(), orderBy(), query(), get(), setAbortAction(), setDisplayValue(), setLimit(), and setValue(). These methods provide functionality for querying, filtering, sorting,

Uploaded by

Prem Kumar Vula
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

SCRIPT INCLUDE&

GLIDE API’S
• Glide APIs : are a set of APIs provided by the ServiceNow platform that allow developers to interact with the platform and
perform a wide range of operations. Glide APIs are built on top of the ServiceNow Glide system, which is a server-side
scripting engine that is used to perform operations on the ServiceNow platform.
• Some of the key features and capabilities of Glide APIs include:
1. Data Access: Glide APIs allow developers to access and manipulate data stored in the ServiceNow platform. This includes the
ability to create, read, update, and delete records in ServiceNow tables, as well as perform advanced operations such as
joining tables, filtering data, and aggregating data.
2. Workflow Management: Glide APIs allow developers to manage workflows and automate processes on the ServiceNow
platform. This includes the ability to create and manage workflows, trigger workflows based on events or conditions, and
monitor the status of workflows.
3. User Interface: Glide APIs allow developers to customize the ServiceNow user interface by creating custom forms, fields,
and UI elements. This includes the ability to create custom UI pages, widgets, and themes, as well as modify the behavior
and appearance of existing UI components.
4. Integration: Glide APIs allow developers to integrate the ServiceNow platform with external systems and services. This
includes the ability to connect to external APIs, consume and publish web services, and interact with other systems using
industry-standard protocols such as REST and SOAP.
• Overall, Glide APIs provide a powerful and flexible set of tools for developers to interact with the ServiceNow platform and
build custom applications, workflows, and integrations. By leveraging Glide APIs, developers can extend the capabilities of
the ServiceNow platform and create custom solutions that meet the specific needs of their organization.
• Glide API’s Method:
• Glide API methods are a set of functions provided by the ServiceNow platform that developers can use to interact with the
platform and perform a wide range of operations. These methods are organized into different categories based on their
functionality and the areas of the platform that they interact with.
• Here are some of the most commonly used Glide API methods:
1. GlideRecord: This API method is used to access and manipulate records in ServiceNow tables. Developers can use this
method to create, read, update, and delete records in tables, as well as perform advanced operations such as joining tables,
filtering data, and aggregating data.
2. GlideSystem: This API method is used to perform system-level operations on the ServiceNow platform. Developers can use
this method to access system properties, log messages, perform date and time calculations, and interact with the file
system.
3. GlideAggregate: This API method is used to perform aggregate operations on ServiceNow tables. Developers can use this
method to calculate sums, averages, and other statistical measures for groups of records in a table.
4. GlideForm: This API method is used to interact with forms and fields on the ServiceNow platform. Developers can use this
method to access form data, validate form data, and set form values programmatically.
5. GlideAjax: This API method is used to make asynchronous calls to the server and retrieve data from the ServiceNow
platform. Developers can use this method to create custom AJAX requests and update the user interface dynamically without
requiring a page refresh.
6. GlideUI: This API method is used to interact with the user interface on the ServiceNow platform. Developers can use this
method to create custom UI components, modify the behavior and appearance of existing UI components, and interact with
UI elements using JavaScript.
• These are just a few examples of the many Glide API methods available on the ServiceNow platform. By using these
methods, developers can extend the capabilities of the platform and create custom applications, workflows, and integrations
that meet the specific needs of their organization.
1. GlideRecord: This API method is used to access and manipulate records in ServiceNow tables. Developers can use this method to
create, read, update, and delete records in tables, as well as perform advanced operations such as joining tables, filtering data,
and aggregating data.
 addActiveQuery(): Adds a filter to return active records.
• addActiveQuery() function that can be used to filter active records in a table.
• The addActiveQuery() function adds an active=true condition to the Glide Record query. This function is typically used when
querying tables that have an active field, such as the Incident [incident] table.
var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.query();
while (gr.next()) {
// do something with the active incident record
gs.print('active incidents are' + gr.number);
}
 The addEncodedQuery():
 that is used to add a pre-defined query to the current Glide Record object.
 This function allows developers to apply complex, pre-built queries to their Glide Record object, without having to manually
construct a query using the addQuery() method.The addEncodedQuery() function takes a single parameter, which is a string
representing the encoded query.
• var gr = new GlideRecord('incident');
• gr.addEncodedQuery("priority=1^OR priority=2");
• gr.query();
• while (gr.next()) {
• gs.print(gr.number);
• }
 addQuery(String name, Object operator, Object value):
 The addQuery(name, operator, value) function in GlideRecord is used to add a new query condition to the GlideRecord object.
 It takes three parameters:
 name: The name of the field that you want to filter on.
 operator: The operator to use in the query. This can be one of the following: <, <=, =, !=, >, >=, LIKE, NOT LIKE, IN, NOT IN, IS, IS NOT,
STARTSWITH, ENDSWITH, CONTAINS, or DOES NOT CONTAIN.
 value: The value that you want to filter on. This can be any valid GlideRecord field value.

var gr = new GlideRecord('incident');


gr.addQuery('priority', '=', 1);
gr.addQuery('caller_id', '=', gs.getUserID());
gr.query();
while (gr.next()) {
gs.print(gr.number);
}
• getRowCount():
• function is used to get the number of records returned by a GlideRecord query. It returns an integer value representing the count of
records returned by the query.
• active incidents with priority 1:
var gr = new GlideRecord('incident');
gr.addQuery('active',true);
gr.addQuery('priority', '1');
gr.query();
var count= gr.getRowCount();
gs.info('There are ' + count + ' active incidents with priority 1');
• getValue():
• is used to get the value of a specific field on the current record. It takes a single argument, which is the name of the field
you want to retrieve the value for, and returns the value of that field
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0000001');
gr.query();
if (gr.next()) {
var shortDescription = gr.getValue('short_description');
gs.info('The short description of incident ' + gr.number + ' is: ' + shortDescription);
}
• getDisplayValue():
• getDisplayValue() function in GlideRecord is used to get the display value of a field in the current GlideRecord record. It takes one parameter
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0000001');
gr.query();
while(gr.next()){
var name= gr.getDisplayValue('caller_id');
gs.info('caller:'+ name);
}
Display the caller name number, shortdesc limit of 32 records?
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.orderByDesc('sys_created_on');
gr.setLimit(32);
gr.query();
while (gr.next()) {
var incidentNumber = gr.getValue('number');
var callerDisplayValue = gr.getDisplayValue('caller_id');
var shortDescription = gr.getValue('short_description');
gs.info('Incident: ' + incidentNumber + ', Caller: ' + callerDisplayValue + ', Short Description: ' + shortDescription);
}
• next():
• Moves to the next record in the GlideRecord.
• orderBy():
• function in ServiceNow GlideRecord is used to specify the order in which records should be retrieved from the database. It takes a
single argument, which is the name of the field you want to use for sorting. By default, records are retrieved in ascending order
based on the specified field.

• Write a script that (orderby) incidents numbers has Priority ?


var gr = new GlideRecord('incident');
gr.orderBy('priority');
gr.query();
while (gr.next()){
gs.info('Incident' + " " + gr.number + " " + 'has Priority' + " "+ gr.priority);
}
• orderByDesc(String, fieldName)
• Specifies the field to use to order the query set in descending order.
• query()
• is a method in the ServiceNow GlideRecord API that performs a query against a table in the ServiceNow database. It takes no
parameters and returns nothing.
• get()
• is a method in the ServiceNow GlideRecord API that retrieves a single record from the database based on the sys_id of the
record. It takes a single parameter, the sys_id of the record to retrieve, and returns a boolean indicating whether the record
was found.
• setAbortAction()
• is a method in the ServiceNow GlideRecord API that specifies what should happen when a database update or insert fails due
to a data or script error.
• setDisplayValue() is a GlideRecord API method that sets the value of a field on a record based on its display value. This
method is used when you want to set a field on a record using a display value instead of its actual value.
• Sets the specified field to the specified display value.
• setLimit(Number limit)
• Sets the maximum number of records to return in the GlideRecord from a query.
• setValue()
a GlideRecord API method that sets the value of a field on a record. This method is used when you want to set a field on a record
using its actual value, rather than its display value.
• var gr = new GlideRecord('incident');
• gr.addQuery('parent_incident','c47ebf892f1221100ec658492799b6af')
• gr.query();
• while (gr.next()){
• gs.info(gr.number);
• }

• Print child incidents


• addJoinQuery() :
• initialize()
• Creates an empty record within the current GlideRecord that is suitable for population before an insert.
• insert()
• Inserts a new record with the field values that have been set for the current record.
• isNewRecord()
• Determines whether the current record has been inserted into the database.
• newRecord()
• Creates a GlideRecord, sets the default values for the fields, and assigns a unique ID to the record.
• update(Object reason)
• Updates the GlideRecord with any changes that have been made. If the record does not exist, it is inserted.
• updateMultiple()
• Updates each GlideRecord in a stated query with a specified set of changes.
• Script Include: In ServiceNow, a script include is a server-side script that can be reused across multiple scripts, such as
business rules, client scripts, UI actions, and other server-side scripts. Script includes provide a way to centralize and reuse
common code and logic, making it easier to maintain and update scripts.
• Script includes are defined as a special type of table in ServiceNow, with the sys_script_include table used to store script
include records. Each script include record contains a script field where the script code is stored. Script includes can be
organized into application scopes, allowing them to be shared across multiple applications and isolated from other
applications.
• Script includes can be created using JavaScript, and can include functions and variables that are specific to the script
include.
• Script includes can be called from other scripts using the global GlideSystem class, which provides a variety of methods for
interacting with script includes.
• Some of the key benefits of using script includes include:
• Code reuse: Script includes provide a way to centralize and reuse common code and logic across multiple scripts, reducing
the amount of duplicated code and making it easier to maintain and update scripts.
• Scalability: Script includes can be used to improve the scalability and performance of ServiceNow applications by reducing
the amount of server-side processing required for each request.
• Modularization: Script includes can be organized into application scopes, allowing them to be shared across multiple
applications and isolated from other applications.
• Testing: Script includes can be tested independently of other scripts, making it easier to identify and fix issues with the
code.
Overall, script includes provide a powerful and flexible way to organize and reuse code in ServiceNow applications, improving
the efficiency, scalability, and maintainability of the platform.
• What is Script Include?*
• Script include is used to store JavaScript that runs on the server.
• Script Include is reusable server-side script logic that defines a function or class. Script Includes execute their script logic on
Server Side only when explicitly called by other script either from Server side or client side.
• There are different types of Script Includes
1. On demand/classless
2. Extend an existing class
3. Define new class
• On Demand/ classless Script
• It Include that defines a single function is known as on demand, or classless Script Include. The function is callable from
other server-side scripts. On demand Script Includes can never be used client-side even if the Client callable option is
selected.
• The script include name must exactly match the name of the function.
• On demand Script Includes are typically used when script logic needs to be reused.
• Examples include standardizing date formats, enabling/disabling logging, and validating email addresses.
ScriptINCLUDE:
1. var HardwareUtils = Class.create();  //
This line creates a new variable called HardwareUtils and sets its value to the result of calling Class.create().
2. HardwareUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {   //
This line sets the prototype property of the HardwareUtils class to an object that extends the AbstractAjaxProcessor class. This means th
at instances of the HardwareUtils class will inherit methods and properties from the AbstractAjaxProcessor class.
3.     getAssignedToName: function() {  //This line defines a new method called getAssignedToName on the HardwareUtils class.
4.         var sysId = this.getParameter('sysparm_sys_id');  //
creates a new variable called sysId and sets its value to the result of calling the getParameter method on the current instance of the Har
dwareUtils class. This method retrieves the value of the sysparm_sys_id parameter from the current AJAX request.
5.         var gr = new GlideRecord('alm_hardware'); //
line creates a new instance of the GlideRecord class, which is used to query records from the ServiceNow platform
6.         gr.addQuery('sys_id', sysId); //
This line adds a new query condition to the GlideRecord. The sys_id parameter specifies that the query should match records where the 
sys_id field is equal to the value of the sysId variable.
7.         gr.query();
8.         while (gr.next()) {
9.             return gr.getDisplayValue('assigned_to'); //
This line retrieves the value of the assigned_to field f.query();rom the current record and returns it as the result of the getAssignedToNa
me method.
10.        }
11.            }, //This line closes the definition of the getAssignedToName method.
12.    type: 'HardwareUtils'  //This line sets the type property of the HardwareUtils class to the string 
13.});
• Client Script:
1. function onChange(control, oldValue, newValue, isLoading) {
2.     if (isLoading || newValue === '') {
3.         return;
4.     } ////This line creates a new instance of the GlideAjax class, which is used to make AJAX requests to the ServiceNow platform. 
5.     var ga = new GlideAjax('HardwareUtils'); //
he 'HardwareUtils' parameter specifies the name of the script include that will handle the AJAX request.
6.     ga.addParam('sysparm_name', 'getAssignedToName'); //
These two lines add parameters to the AJAX request. The 'sysparm_name' parameter specifies the name 
7.     ga.addParam('sysparm_sys_id', newValue);//
The 'sysparm_sys_id' parameter specifies the value that should be passed as an argument to the function. In this case, it is the value of 
the newValue parameter.
8.     ga.getXML(callBack); //
This line sends the AJAX request to the ServiceNow platform and specifies that the callBack function should be called when the respons
e is received.        
9.         function callBack(response) { // The response parameter is an object that contains information about the response
10.         var answer = response.responseXML.documentElement.getAttribute('answer');  //
The response.responseXML property is an XML document that contains the response data. The responseXML.documentElement.getAttrib
ute('answer') method retrieves the value of the answer attribute from the root element of the response XML
11.         g_form.setValue('assigned_to', answer); //
This value is then passed to the g_form.setValue('assigned_to', answer) method, which sets the value of the assigned_to field on the for
m to the value of the answer attribute from the response.
12.     }
13. }
GLIDE AGGREGATE:
1. Count The Total No.Of Incidents? By using addAggregate?
• var ga = new  GlideAggregate('incident');
• ga.addAggregate('COUNT');
• ga.query();
• while(ga.next()){
•     var total = ga.getAggregate('COUNT');
•     gs.print('Total NO.OF Incidents:' + total);
• }

2. Using addEncodedQuery display Total number of incidents with category "servicenow" and priority 1?
• var agg = new GlideAggregate('incident');
• agg.addEncodedQuery('category=servicenow^priority=1^ORDERBYstate');
• agg.addAggregate('COUNT');
• agg.query();
• if (agg.next()) {
•   var totalCount = agg.getAggregate('COUNT');
•   gs.print('Total number of incidents with category "servicenow" and priority 1: ' + totalCount);
• }
• Print description and short description for any form(inc.prblm,change) by using background scripts?
• run a query in script background to show all the active incidents?

You might also like