100% found this document useful (1 vote)
644 views33 pages

Gliderecord

The document provides an outline and overview of using the GlideRecord API in ServiceNow for querying and accessing database records. It covers the two main stages of using GlideRecord - building queries and processing records returned. Key topics include common GlideRecord methods, using chained methods and encoded queries to build queries, iterating through records using next(), and accessing fields of individual records.

Uploaded by

Havi Kosuru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
644 views33 pages

Gliderecord

The document provides an outline and overview of using the GlideRecord API in ServiceNow for querying and accessing database records. It covers the two main stages of using GlideRecord - building queries and processing records returned. Key topics include common GlideRecord methods, using chained methods and encoded queries to build queries, iterating through records using next(), and accessing fields of individual records.

Uploaded by

Havi Kosuru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Course Outline

1 Course Introduction 6 GlideForm & GlideUser

2 Development Overview 7 GlideAjax

3 Scripting Locations 8 Exploring Other APIs

4 GlideRecord 9 Becoming A Scripting Master

5 GlideSystem 10 Creating A Custom App


Section Outline
1 GlideRecord Introduction 7 Walking Through CRUD

2 Show Me The Code! 8 GlideRecord Demo

3 Concept: Dot-Walking 9 GlideRecordSecure

4 GlideRecord API Diagram 10 GlideAggregate

5 Common GlideRecord Methods 11 Where Can I Use This?

6 Stages Of A GlideRecord 12 Section Recap


Application Storage

GlideRecord
GlideRecord Introduction
● Most common API
● Server side 1 2
● Used for database operations (CRUD)
● Generates SQL for you
● 2 stages
○ Building a query
○ Process records

C reate R ead U pdate D elete


Show Me The Code!
● Print a list of all priority 1 incidents to the screen

Best Practice: Use meaningful variable names with context


GlideRecord By Analogy: Grocery Shopping

1. Go to a grocery store
2. Grab a shopping cart
3. Place groceries in the shopping cart
4. Checkout at cashier

Records

Table GlideRecord Action


General GlideRecord Steps

1 Select a table Incident 1 Build Query Stage

2 Build a filter All priority 1 incidents

3 Query table

2 Process Records Stage 4 Process rows Print number to screen

Note: Any ServiceNow table may be queried with GlideRecord


Concept: Dot-Walking

● Relational database
● Reference fields starting_table reference_field reference_field

Table A Table B Table C


Field 1 Field 1 Field 1
Field 2 Field 2 Field 2
Field 3 Field 3 Field 3
Reference 1 Reference 1
Reference 2

Table D
Field 1
Field 2
Dive Deeper: Checkout this wiki article for a deeper understanding on primary keys
Example: Dot-Walking

● Example:
○ For a specific incident, you would like to find the location of the caller

incident sys_user cmn_location


GlideRecord API Diagram
GlideRecord

addQuery() next()
table

setLimit() getRowCount()

query
addEncodedQuery() hasNext()

addNotNullQuery() deleteRecord()
records

query() newRecord()

fields
orderBy() update()

Note: Ovals represent properties or topics associated with API, while rounded rectangles represent methods
GlideRecord API Mapping

GlideRecord

table

addQuery()

query

query()

records

next()

fields
Common GlideRecord Methods

● query() ● get()
● newRecord() ● orderBy()
● insert() ● orderByDesc()
● update() ● canCreate()
● deleteRecord() ● canWrite()
● addQuery() ● canRead()
● addEncodedQuery() ● canDelete()
● hasNext()
● next()
GlideRecord Stage 1: Building Query
1 Build Query Stage

GlideRecord

table

addQuery()

query

query()

records GlideRecord = { SELECT *


table: ‘incident’, FROM GlideRecord.table
next() query: ‘priority=1’, WHERE GlideRecord.query
...
};
fields
GlideRecord Stage 1: Options To Build Queries

1 Chain Methods 2 Encoded Query

● addQuery() ● addEncodedQuery()
● addOrCondition()
● addNullQuery()
● addNotNullQuery()
● addActiveQuery()
● addInactiveQuery()
GlideRecord Stage 1: Option 1 - Chain Methods 1

1 Chain Methods

Add GlideRecord methods onto the


current GlideRecord object
GlideRecord addQuery() Method 1

● Accepts 2 or 3 arguments

2 Arguments 3 Arguments

field_name field_value field_name operator field_value

Numbers Strings

=
Note: ‘=’ is assumed if there are only 2 arguments
= >= > !=
STARTSWITH DOES NOT CONTAIN
ENDSWITH INSTANCEOF
!= <= < IN
CONTAINS
NOTIN
GlideRecord Stage 1: Option 2 - Encoded Query 1

1 Build Query 2 Copy Query

priority=1^ORpriority=2^category
=hardware^ORcategory=software^sy
s_created_on>javascript:gs.dateG
enerate('2017-01-01','12:00:00')
^short_descriptionISNOTEMPTY

3 Add to addEncodedQuery()
GlideRecord Stage 2: Process Records
2 Process Records Stage

GlideRecord

table

addQuery()

query

query()

records
{
next() number: ‘INC00001’,
short_description: ‘Test’,
...
fields
}
GlideRecord next() Method & Iteration 2

incidentGR Incident 1 Incident 2 Incident 3 Incident 4

sys_id: ‘08ed3ab…’
sys_id: ‘a37ed1...’
sys_id: ‘b90234...’
sys_id: ‘e3294da...’
number: ‘INC000123’
number: ‘INC000124’
number: ‘INC000125’
number: ‘INC000126’
state: ‘open’ state: ‘closed’ state: ‘pending’ state: ‘open’
short_description: ‘Test1’
short_description:
short_description:
‘Test2’ short_description:
‘Test3’ ‘Test4’

Dive Deeper: Checkout this wiki article and this YouTube video if you’d like to learn more on iterators
Accessing A Record’s Fields 2

● Once query() method is executed and stage 2


begins, all fields are just a dot away
● Fields become GlideRecord properties
number

short_description

sys_created_on
incidentGR

caller_id

[field_name]
GlideRecord get() Method 1 2

● Shortcut
● Only grabs 1 record
● Commonly used with record sys_id

GlideRecord

table query

records fields

get()
CRUD GlideRecord Mapping

.insert() .query()* .update() .deleteRecord()

C reate R ead U pdate D elete


CRUD - Create

1. Build GlideRecord
2. query()
3. newRecord()
4. Set field values
5. insert()
CRUD - Read

1. Build GlideRecord
2. Add filter conditions (optional)
3. query()
4. next()
5. Print or copy variables
CRUD - Update

1. Build GlideRecord
2. Add filter conditions (optional)
3. query()
4. next()
5. Set field values
6. update()
CRUD - Delete

1. Build GlideRecord
2. Add filter conditions (optional)
3. query()
4. next()
5. deleteRecord()
GlideRecordSecure

● GlideRecordSecure class is inherited from GlideRecord


○ Has all of the same methods
○ Performs ACL checking
● Used to secure Script Includes
● Replaces canWrite(), canRead(), canUpdate(),
canDelete() GlideRecord methods

Dive Deeper: Watch episode 15 of TechNow to learn more about GlideRecordSecure


GlideRecord Versus GlideRecordSecure

vs
GlideAggregate

● Extension of GlideRecord class


● Used when performing aggregate queries (count, min, max, sum, avg)
○ Reports
○ Calculations
● Only works on number fields
Where Can I Use This?
Client-Side Server-Side

GlideForm GlideRecord

GlideUser Client Scripts UI Actions GlideSystem Business Rules UI Actions

GlideAjax GlideDateTime

UI Policies Service Portal Script Includes Scheduled Jobs

Service Portal Web Services Workflows

Note: GlideRecord on the client side is not recommended, thus will not be discussed in this course
Section Recap

● Use GlideRecord when dealing with database operations


● CRUD stands for Create, Read, Update, Delete
● GlideRecord is a server-side API
● There are 2 stages to the GlideRecord API
○ 1) Query building stage
○ 2) Process records stage
● Use the next() method to iterate over the returned records
● Use the get() method to retrieve a specific record using a
unique field, such as the sys_id
Section Recap (cont.)

● Use GlideAggregate over GlideRecord when dealing


with aggregates like counts
● Log records before deleting
● Avoid using GlideRecord in Client Scripts
● Use GlideRecordSecure where appropriate

You might also like