# ServiceNow UI Developer cheat sheet
-----------------------------------------------------------------------------------------
### Utility URLs
URL | Purpose
--- | -------
/stats.do | Quick stats
/cache.do | Clear your instance cache
/cancel_my_transaction | Cancel your currently running transaction
/cache_inspect.do | Inspect the content of various system caches
/$restapi.do | Rest API explorer
-----------------------------------------------------------------------------------------
### Common directories
Path | Description
---- | -----------
/ui.html | Static files accessible from /
/ui.jtemplates | Jelly templates
/ui.jforms | Jelly forms accessible from /
/ui.jtemplates/doctype | Doctype Jelly template overrides
/ui.jforms/doctype | Doctype Jelly form overrides
-----------------------------------------------------------------------------------------
### URL resolution
1. Virtual host provider
1. Static content
1. Processor
1. Processor table
1. Parameter - incident.do?**PARAMETER**
1. Path - **PATH**.do
1. Extension - incident.**EXTENSION**
1. Or a com.glide.processors extension point declared in plugin.xml
1. UI Page?
1. Jelly Form?
1. Table?
1. Table with list suffix?
1. Search suffix?
1. Update suffix?
1. :frowning_face: not_found.xml
-----------------------------------------------------------------------------------------
### Direct UI page (no page template)
- Jelly forms with $ prefix
- $my_form.xml => /$my_form.do
- ?sysparm_direct=true
- $my_ui_page.do?sysparm_direct=true
- UI Pages marked direct
-----------------------------------------------------------------------------------------
### Accessing URL Parameters
- RP.getParameterValue(String param)
- ${sysparm_*} (Parameter must start with sysparm_)
-----------------------------------------------------------------------------------------
### Front end scripting
Selector | Usage
--- | ---
$(id) | prototypeJS getElementById()
$$(cssRule) | prototypeJS css selector
$j(selector) | jQuery
#### Common client scripting APIs
API | Description
--- | ---
GlideAjax | Access a service side script include
GlideURL | Create and parse urls
GlideModalForm | Display a form in a modal
g_form | GlideForm shortcut for interacting with a form
g_user | GlideUser shortcut to the current user
-----------------------------------------------------------------------------------------
### Jelly scripting
Syntax | Phase
--- | ---
${ ... } | Phase 1
$[ ... ] | Phase 2
#### JEXL vs JavaScript
JEXL expressions are not JavaScript and Jelly variables are not Rhino variables. You cannot mix them.
JEXL lets you access Rhino scriptable methods and fields like **gs.log()** and **gr.next()**.
#### JEXL methods
Method | Usage | Description
--- | --- | ---
size() | ${ **size**(_jvar_) } | for String, counts length
for Map, counts keys
for List, counts elements
empty() | ${ **empty**(_jvar_) } | True if:
- null, empty string
- a zero length collection
- a map with no keys
- an empty array
.startsWith() | ${ _jvar_.**startsWith**("hello") } | returns bool
.endsWith() | ${ _jvar_.**endsWith**("world") } | returns bool
#### Jexl helpers
${AND} => &&
${LT} => <
${GT} => >
$[SP] =>
-----------------------------------------------------------------------------------------
### Jelly Escaping
JEXL output is automatically escaped. Use these prefixes to manually control how output is escaped. Refer to GlideExpressionWrapper for more detail.
```
${HTML,JS:gr.getDisplayValue('short_description')}
```
Prefix | When to use
--- | ---
HTML | HTML escaping
JS | JavaScript escaping
JS_STRING | Escape a string containing JavaScript
NS | Escapes script tags for XSS prevention
JS_XML | Escaping for XML used as a javascript string
URL | For URLs
NLBR | Newline to <br/>
WBR | Insert <shy; in long sequences the browser would not break
LINK | Convert patterns looking like hyperlinks to actual ones <a>
HTMLSAN | Sanitizes suspicious HTML with JellyXMLSanitizer
NG | sanitizes against angular expressions
NOESC | Disables escaping for the specified string
SAFE | No JavaScript interpolation protection
HTML
& => &
< => <
> => >
JS
' => \'
" => \"
CR => blank
NL => \n (the string "\n" so that javascript interprets it as a NL)
=> <script...>
=> </script...>
if phase 1:
& => &
< => \\u003C
JS_STRING
Same as JS escaping except that less than (<) is not escaped.
NS
Note: this is only done if glide.ui.escape_text is false.
=> <script...>
=> </script...>
JS_XML
' => \'
CR => blank
NL => \n (the string "\n" so that javascript interprets it as a NL)
=> <script...>
=> </script...>
< => \\u003C
> => \\u003E
if phase 1:
& => &
URL
http://localhost:8080/nav_to.do?uri=sys_ui_page.do?sys_id=zzz => http%3A%2F%2Flocalhost%3A8080%2Fnav_to.do%3Furi%3Dsys_ui_page.do%3Fsys_id%3Dzzz
NLBR
"This\nis\na\ntest" => This
<strong>is</strong>
a
test
WBR
Word break, insert <shy; in long sequences the browser would not break
LINK
Convert patterns looking like hyperlinks to actual ones
HTMLSAN
Sanitizes suspicious HTML with JellyXMLSanitizer.
NG
Sanitizes against angular expressions
{{ => {\\u200b{
NOESC
No escaping
SAFE
No JavaScript interpolation protection
**Manually Sanitize output**
```
SNC.GlideHTMLSanitizer.sanitizeWithConfig("HTMLSanitizerConfig","Your text here")
```
-----------------------------------------------------------------------------------------
### Jelly Tags
Tag | Description
--- | ---
```...``` | Execute block if **test** is:
- a Boolean and true OR
- a String and = "true", "yes", "on", or "1"
```...``` | Repeat block until **test** is false
`````` | Set **var** to **value** or **defaultValue** if **value** is empty
`````` | Set **var** to expression in **true** or **false** depending on result of **test**
`````` | Combination of a switch statement and if/else if statement, uses the first **when** block where **test** is true, **otherwise** defines the default case
`````` | Insert a jTemplate or UI Macro at this position with access to surrounding Jelly context
`````` | Similar to g:inline but can't access Jelly variables from the surrounding context
```...``` | Evaluate code block in Rhino and assign result to **var**
**jelly** = true to access jelly variables inside script
**copyToPhase2** = true to make **var** available in phase 2 context
`````` | Same as above except self closing usage. Evaluate **expression** in Rhino and assign result to **var**
```...``` | Disables automatic output escaping of all contained ${} expressions
`````` | Dumps all of the current jelly variables to the debug ouput
Or a single variable if specified with **var**
If Tag:
...do something
While tag:
Set tag:
Set_if tag:
Choose tag:
Anything
Anything
World
The default case
Insert tag:
Inline tag
Evaluate tag:
Math.random();
jvar_result will contain the result of the evaluate tag. var = The last line of code in the evaluate block.
A single line usage of evaluate. Evaluates the contents of expression.
To treat the returned evaluation as an object, set object="true". Use when returning anything other than string or int.
To use Jelly variables inside your Rhino script, set jelly="true". Any jelly variable will be accessible on the jelly object inside the code block.
To use the result of the expression in phase 2 like this $[jvar_result], set copyToPhase2="true".
-----------------------------------------------------------------------------------------
### Common System Properties
Property name | Description
--- | ---
glide.ui.template.trace | true = Display jelly template tracing output in log
full = Include full template path in tracing output
false = Disable template tracing
glide.ui.js_includes | true = Bundle JS includes into a few large files
false = Load JS files separately (useful for debugging)
glide.ui.session_timeout | Session timeout in minutes
glide.ui.i18n_test | true = Adds a prefix to each translated message and field
false = Off
glide.sys.date_format | Customize the system default date format
glide.sys.time_format | Customize the system default time format
#### Session Properties vs Database Properties
To change a property for just the current user session use:
GlideProperties.set('key', 'value');
To change a system property for everyone, and modify the sys_property table use:
gs.setProperty('key', 'value'); // You must have maint role to do this
-----------------------------------------------------------------------------------------
### Common Glide Server globals
Variable | Description
--- | ---
action | Action handler (only available if running in a UI action)
current | Current record as GlideRecord
previous | The current record prior to any updates being applied
g_scratchpad | This object is serialized and passed to the client at the end of the transaction
gs | GlideSystem
RP | RenderProperties
ref | The table name of the current record
-----------------------------------------------------------------------------------------
### Common Glide Server Scripting APIs
- JSUtil
- GlideRecord
- GlideUser (gs.getUser())
- GlideSession (gs.getSession())
- GlideProperties
-----------------------------------------------------------------------------------------
### GlideSystem (gs) server scripting APIs
API | Description
--- | ---
.addInfoMessage(String) | Display an info message on the client.
.addErrorMessage(String) | Display an error message on the client.
.getMessage(String|Array) | Get a translated message for a key or key[]
.getProperty(String key, Object default) | Get a system property value or use default if not found.
.nil(Object) | Returns true if the object is null, undefined or an empty string.
.base64Encode(String) | Returns a base64 encoded string.
.base64Decode(String) | Returns an ASCII string from a base64 encoded string.
.info(String message, Object, Object, Object...) | Writes a message to the system log
.print(String) | Prints a message to the debug console
.getSession() | Returns the current glide session.
.getUser() | Returns the GlideUser of the current user.
.getUserName() | Returns the name of the current user.
.urlEncode(String) | Returns a UTF-8 encoded string.
.urlDecode(String) | Returns an ASCII string from a UTF-8 encoded string.
-----------------------------------------------------------------------------------------