Scripting class Flashcards
Client side scripts are run on the browser and handle what kind of functions?
They can auto populate a field based on the value of another field
Show or hide form sections
They have access to data on forms and in lists
Server side scripts are generally used for what purpose
These typicall modify a database record or generate an event
These have access to the actual database unlike client side scripts
MID server scripts are generally used for what purpose
to integrate to a 3rd party application
The syntax editor uses color coding including Green, Purple, and Blue. What do each of these colors mean?
Green are comments
Purple are JavaScript commands
Blue are strings and reserved words
What is the keyboard shortcut to display a list of valid elements at the cursor’s current position?
control + spacebar at the beginning of a line
What is the keyboard shortcut to list methods for a class?
period after a valid class name
What is the keyboard shortcut to list expected parameters
Open parenthesis after a valid class, function, or method name
Do properties that have been locally declared appear in the context-sensitive help?
Yes they do
Is the following class used on the client or server side:
GlideAjax
Client Side
Is the following class used on the client or server side:
Glide Aggregate
Server - Side
Is the following class used on the client or server side:
GlideForm
Client - Side
Is the following class used on the client or server side:
GlideDateTime
Server Side
Is the following class used on the client or server side:
GlideList
Client-side
Is the following class used on the client or server side:
GlideElement
Server - side
Is the following class used on the client or server side:
GlideRecord
Client - Side
Is the following class used on the client or server side:
GlideRecord
Server - Side
Is the following class used on the client or server side:
GlideUser
Client-Side
Is the following class used on the client or server side:
GlideSystem
Server-Side
Is the following class used on the client or server side:
spModal
Client-Side
Is the following class used on the client or server side:
JSON
Server-Side
Is the following class used on the client or server side:
Workflow
Server-side
A client script manages the behavior of forms, fields, and lists in real time including the following:
- Make fields mandatory
- set one field in response to another
- modify choice of list options
- hide/show form sections
- display an alert
- hide fields
- prohibit list editing
These execute client side
When there are multiple client scripts executing what order do they go in?
This is only an issue if they are on the same table and they will execute based on their order number from lowest to highest.
How does an onLoad() client script work
This script runs when a form loads and before control is given to the user. It is typically used to manipulate a form’s appearance or content on screen.
Users are not able to modify the form while an onLoad script executes
How does an onSubmit() script work
This runs when a form is saved, updated, or submitted and is typically used for field validation
What does the following code do:
function onSubmit() {
if (!myCondition) {
return false;
else {
//perform some logic here
}
}
This runs on form submission and will cancel the submission if “myCondition” isn’t met. Return false is what cancels the process.
How does an onChange() script work
This script runs when a particular field value changes. It is typically used to respond to field values of interest, modify one field value in response to another. An onchange script can only watch one field at a time.
What are the five parameters automatically passed to an onChange client script and what do they do?
- control - name of the object(field_name) whose value just changed. The object is identified in the Field name field on the Client Script form.
- oldValue - value of the control field when the form loaded and prior to the change. The old value is always what it was when the form loaded no matter how many times it is changed
- newValue - value of the control field after the change
- isLoading - boolean value indicating whether the change is occuring as part of a form load. Value is true if chane is due to a form load. A form load means all of a form’s field values changed
- isTemplate - boolean value indicating whether the change occurred due to population of the field by a template. Value is true if change is due to population from a template.
How does the onCellEdit() script work?
Script runs when a particular field value on a list changes. Applies to all records selected. This can be used to ensure data being controlled via scripts on a form has similar constraints on a list view.
Does not apply to widgets on homepages or dashboards.
what parameters are automatically passed to an onCellEdit() Client Script
- sysIDs - sys_id of the edited item(s)
- table - the table name of the edited item(s)
- oldValues - the old value of the edited cell(s)
- newValue - the new value of the edited cell(s). Is the same for all edited items.
- callback - a callback will continue the execution of other related cell edit scripts
what does the following function do:

This script defaults to save and close but if the new value of state is either resolved or closed the value of save and close is changed to false. When this happens the callback function that calls the variable gets the new value of false meaning it won’t run and it also alerts the user that they are unable to make that change.
What are the three most popular client-side APIs
g_form - object whose properties are methods used to manage a form and its fields in the record
g_user - object whose properties contain session information about the currently logged in user and their role(s)
g_scratchpad - object passed to a client script from a server-side script known as a display business rule. The object’s properties and values are determined by the server side script.
What can you use to access GlideForm methods
g_form global object
for example:
g_form.setValue(‘impact’,1);
g_form.showFieldMsg(‘state’,’Change is waiting approval’,’info’);
What g_form methods are used to:
Draw attention to an area on the form
flash() - flashes a field’s label to draw attention to it
showFieldMsg() - displays a message under a form field
What g_form methods are used to:
Get field information
getValue() - retrieves a field’s value
getReference() - retrieves a reference object from the database
What g_form methods are used to:
Change a field value
setValue() - sets a field’s value
clearValue() - clears a field’s value
What g_form methods are used to:
Change a choice list
addOption() - adds an option to the end of a Choice list
clearOptions() - removes all options from a Choice list
What g_form methods are used to:
get form information
getSections() - retuns the elements of a form’s section as an array
isNewRecord() - returns true if a record has never been saved
What g_form methods are used to:
generate form actions
addInfoMessage() - displays an informational message at the top of a form
clearMessages() - removes messags previously added to the form
What does g_form.getValue retrieve and what is something that needs to be watched when using this method
This returns a string despite the data type of the field. If returning a number is important use the g_form.getIntValue() or g_form.getDecimalValue()
what does the g_user object do?
This GlideUser API provides useful methods to access information about the currently logged in user
the following are examples of what:
firstName
lastName
userID
userName
these are g_user properties
what is the try/catch syntax and what does it do
This is a debugging tool used to capture runtime errors and is written as follows:
try {
//code to execute goes here
}
catch(err) {
//code to deal with error here
}
What does the following script do:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == ‘’) {
return;
}
var incState = g_form.getValue(‘incident’);
jslog(“<your_initials> The value of incState is: " + incState);</your_initials>
if (incState == 1) {
jslog(“<your_initials> LINE 11 EXECUTED!");</your_initials>
g_form.addDecoration(‘state’, ‘icon-edit’,’Gathering initital details’);
g_form.flash(‘state’, “teal”, -4);
}
First this code logs the value of incState from the form on change.
If it finds that incState == 1 it will report the value to ou then add a decoration and a flash.
what debugging tools are only available to desktop
the JavaScript log and Field Watcher
the g_form methods and alert arguments can be what?
strings, g_form properties or methods, g_user properties or methods, variables, JavaScript string escape characters such as \n (new line)(ignored for g_form methods)
What occurs when a script requests a record on another table in the database
- A client-side script requests a record from the database
- The server retrieves the requested record from the database and passes the record back to the calling client-side script as an object
- The callback function logic executes when the object returns from the server
What does a callback function do in a script
the g_form.getReference() method runs asynchronously when a callback function is used. When the record is returned to the calling script, the logic in the callback function executes. If the callback function is omitted, the g_form.getReference() method runs synchronously.
What is the difference between synchronous and asynchronous
Synchronous getReference() method calls lock the form until the requested record is returned from the database. Asynchronous execution allows uers to continue using the form while the method executes.
What kind of code is the following:
var userRecord = g_form.getReference(‘caller_id’,callerInfo);
function callerInfo (userRecord) {
alert(“User ID: “ + userRecord.user_name + “\nLocked Out:” + userRecord.locked_out);
}
this is an Asynchronous code that first gets the user info and then puts it into a function
What do g_form methods do?
These are client scripts that are used to manage the form and form fields
what do g_user properties and methods do
These are part of a client script and are used to access information about the currently logged in user
What does a UI policy do
defines the behavior and visibility of fields on a form:
mandatory
visible
read-only
condition must be true for it to execute
execute after client scripts
What data can you see in a UI policy
local variables declared in a script, predfined client-side global variables such as
g_form (glideForm)
g_user(GlideUser)
g_scratchpad(in conjunction with a Display Business Rule, Module 5)
What is a business rule
JavaScript that runs when -
a record is:
- displayed
- inserted
- updated
- deleted
or a table is queried
What kind of business rule would do the following:
Prevent users from one location from seeing CIs from another
A before business rule as this would run before the query is sent to the database
what are the pre-defined server side global variables
- current - is an object that stores the current record’s fields and values; new/modified values. A script can change a field’s value many times, current will store the most recent value. For example: if (previous.priority == current.priority { return; }
- previous - is an object that stores the record’s fields and values before any changes were made; the original values when the form loaded. Reference this object using previous.<field_name>. For example: if (previous.priority == 1) { //logic here }</field_name>
- g_scratchpad - display business rules only, works in conjunction with a client-side script
What is dot walking
This is a way to allow direct access to fields for related records (reference objects) by traversing tables
<object>.<reference>.<field></field></reference></object>
What is the “Script tree”
This is a tool used for dot walking that can be expanded while writing scripts.
A table icon indicates the field references another table. Expand the reference object to select the field of interest from that table.
A blue circle indicates the field that resides on the current table
A red square indicates the field resides on a parent table
An index field icon indicates the field is part of one or more indexes
Glide system logging methods sending debugging information to the system log or the top of the form. What are they?
the system log debuggers include:
- gs.log()
- gs.error()
- gs.info()
- gs.warn()
The top of the form debuggers include:
- gs.addInfoMessage()
- gs.addErrorMessage()
What do i do to pass data from the server side to the client side during a form load
Display business rules
What do user methods do and what are some examples of them
they return information about the currently logged in user
Examples:
- getUser() - returns a reference to the User object for the current user
- getUserName() - returns the username of the current user (fred.luddy)
- getUserID() - returns the sys_id of the current user
- getUserDisplayName() - returns the name field of the current user (Fred Luddy instead of fred.luddy)
- hasRole() - returns true if the user has the specified role
- hasRoleInGroup() - returns true if the user has the specified role within a specified group
What will the following script return:
if(gs.hasRole(‘itil’)){
- gs.addInfoMessage(“getUser() returns: “ + gs.getUser());
- gs.addInfoMessage(“getUserName() returns: “ + gs.getUserName());
- gs.addInfoMessage(“getUserID() returns: “ + gs.getUserID());
- gs.addInfoMessage(“getUserDisplayName() returns: “ + gs.getUserDisplayName());
}
- getUser() returns: come.glide.sys.User@36426f72
- getUserName() returns: beth.anglin
- getUserID() returns: 4644a23a9fe19810012d100cca8066
- getUserDisplayName() returns: Beth Anglin
addInfoMessage() - adds a blue informatial message to the top of a form or list
addErrorMessage() - adds a red warning message to the top of a form or list
What are system methods and some examples of them
System methods primarily work with form objects, tables, fields, and logging. Examples include the following:
- getProperty() - returns the value of a Glide property
- getPreference() - gets a user preference
- getDisplayColumn() - gets the display column for the table
- tableExists() - determines if a database table exists
- nil() - returns true if a field’s value is null or an empty string(“”)
- eventQueue() - queues an event for the event manager
- print() - writes a message to the system log
- log() - logs a message to the system log and saves it to the syslog table
- logError () - logs an error to the system log and saves it to the syslog table
- getMessage() - retrieves translated messages to display in the UI.
Date and Time methods work with date and time data. What are some examples of these methods?
beginningOfLastWeek() - date and time for the beginning of last week in GMT (yyyy-mm-dd hh:mm:ss)
beginningOfNextMonth() - date and time for the beginning of next month in GMT
dateDiff() - calculates the difference between two dates
endOfLastWeek() - date and time for the end of last week in GMT
endOfNextMonth() - date and time for the end of next month in GMT
minutesAgo()/quartersAgo()/monthsAgo()/yearsAgo() - returns the date and time a specified number of minutes/quarters/months/years ago
now() - current date
nowDateTime() - current date and time in the user-defined format
What does the following code do?
var firstDay = current.u_hire_date.getDisplayValue();
var lastDay = current.u_last_date.getDisplayValue();
if(firstDay && lastDay) {
if(gs.dateDiff(firstDay, lastDay, true) < 0) {
gs. addErrorMessage(“Add error message here”);
current. u_last_date = ‘’;
current. setAbortAction(true);
}
}
This checks to see if first day and last day have values, and if they do it makes sure the lastDay isn’t before the firstday
What is a display business rule?
rules to pass data from the server-side to the client-side during a form load
What does the glide system API do
it’s a collection of methods, executes server side, accesses system-level information such as:
- logged in user
- system
- date and time
What do the following user methods do:
getUser()
getUserDisplayName()
getUserID()
getUserName()
hasRole()
hasRoleInGroup()
getUser() - returns a refernce to the user object for the current user.
getUserDisplayName() - returns the name field of the current user (Fred Luddy instead of fred.luddy)
getUserID() - returns the sys_id of the current user
getUserName() - returns the username of the current user (fred.luddy)
hasRole() - returns true if the user has the specified role
hasRoleInGroup() - reutrns true if the user has the specified role within a specified group
What are system methods
These primarily work with form objects, tables, fields, and logging
examples:
- getProperty(), getPreferece()
- getDisplayColumn(), tableExists()
- nil(), eventQueue()
- print(), log(), logError(), getMessage()
What do the following system methods do:
- getProperty()
- getPreference()
- getDisplayColumn()
- tableExists()
- nil()
- eventQueue()
- print()
- log()
- logError()
- getMessage()
- getProperty() - returns the value of a Glide property
- getPreference() - gets a user preference
- getDisplayColumn() - gets the display column for the table
- tableExists() - determines if a database table exists
- nil() - returns true if a field’s value is null or an empty string(“”)
- eventQueue() - queues an event for the event manager
- print() - writes a message to the system log
- log() - logs a message to the system log and saves it to the syslog table
- logError() - logs an error to the system log and saves it to the syslog table
- getMessage() - retrieves translated messages to display in the UI
what are date and time methods
work with date and time data:
examples:
- beginningOfLastWeek()/endOfLastWeek(), beginningOfNextMonth()/endOfNextMonth()
- dateDiff()
- now(), nowDateTime()
- minutesAgo(), quartersAgo(), monthsAgo(), yearsAgo()
what is a gliderecord
- used for database operations instead of writing SQL queries
- an object containing zero or more records from the same table (ordered list)
- properties are the table’s field names (columns)
- execute server-side
any table row from any table can become a GlideRecord
GlideRecord queries can be called client-side but this should be avoided due to performance impact. The getReference() method previously discussed in Module 3 - Client Scripts performs a GlideRecord query
What is the strategy for creating a new GlideRecord query
- Create a GlideRecord object for the table of interest
- Build the query condition(s)
- Execute the query
- Process returned records with script logic
What are the four steps to using a glide record and their code
- Create a GlideRecord object for the table of interest
- var myObj = new GlideRecord(‘table_name’);
- Build the query condition(s)
- myObj.addQuery(‘filed_name’, ‘operator’, ‘value’);
- myObj.addQuery(‘field_name’, ‘operator’, ‘value’);
- Execute the query
- myObj.query();
- Process returned record(s) with script logic
- while(myObj.next()) {
Step 1: Create a GlideRecord Object for the Table of Interest
- To query a table, first create an object for the table
- Object is called a GlideRecord
- Only parameter needed is the name of the table
var myObj = new GlideRecord(‘table_name’); <— Syntax
var myObj = new GlideRecord(‘change_request’); <—Example
What is the code for the following portion of creating a glide record
Step 2: Build the Query Condition(s)
- addQuery() builds a SQL select statement (not seen by the user)
- Each addQuery() call adds a new “where” clause to the select statement
- addQuery() calls are automatically AND’ed
var myObj = new GlideRecord(‘change_request’);
myObj.addQuery(‘category’, ‘=’, ‘Hardware’);
myObj.addQuery(‘priority’, ‘!=’, 1);
select * from change_request where category = hardware and priorty != 1;
What is the code for the following:
Step 3: Execute the query
myObj.query();
What is the code for the following:
Step 4: Process returned records with script logic
next() moves to the next record in the GlideRecord object
while() iterates through all returned records
if() process only the first record returned
hasNext() steps through returned records and determines if there are any more records
what does the following code do:
var incGR = new GlideRecord(‘incident’);
incGR.addQuery(‘problem_id’, ‘=’,current.sys_id);
incGR.query();
while (incGR.next()) {
incGR.work_notes = “related problem “ + current.number + “ closed with the following close notes: \n\n” +
current.close_notes;
incGR.update();
}
The update() method to save changes to the records in the GlideRecord
If record does not exist, it is inserted
The work_notes fields in the returned Incident records are updated with the value currently in the related Problem’s close_notes field
What are the four steps in the GlideRecord creation?
- Create a GlideRecord object for the table of interest
- Build the query condition(s)
- Execute the query
- Process returned record(s) with script logic
what is an addOrCondition()
use the addOrCondition() method to add a new condition to a select statement using OR
works with addQuery()
var myObj = new GlideRecord(‘change_request’);
var q1 = myObj.addQuery(‘category’, ‘=’, ‘Hardware’);
q1.addOrCondition(‘priority’, ‘!=’, 1);
select* from change_request where category = ‘Hardware’ OR priority !=1;
use an object variable and addQuery() to add the first condition and addOrCondition for the second conditon
how does the glide record mixing ANDs and ORs work
Create “groups” to represent()
(A or B) and (C or D)
var myObj = new GlideRecord(‘incident’);
var q1 = myObj.addQuery(‘state’, ‘<’, 3);
q1.addOrCondition(‘state’, ‘>’, 5);
var q2 = myObj.addQuery(‘priority’, 1);
q2.addOrCondition(‘priority’, 5);
myObj.query();
select * from incident where (state<3 OR state>5) AND (priority = 1 OR priority = 5);
How do i code a query for a single record?
get(object name, Object value)
used to query for a single record
returns:
true - if a record is found
false - if a record is not found
if(!current.parent_incident.nil()) {
var myObj = new GlideRecord(‘incident’);
myObj.get(current.parent_incident);
myObj.u_rca = true;
myObj.update();
}