ServiceNow Scripting Fundamentals Flashcards
When should you script? (5)
- Add new functionality
- Extend existing functionality
- Guide users through messaging (alerts/notification)
- Automate processes
- Interact with third party applications
Which administrator develops in the global scope?
System Administrator
Which administrator develops in a certain piece (like business rules), typically within a certain application?
System Definition Administrator
What is the name of ServiceNow’s built in text editor and what are it’s features?
Syntax Editor:
- Automatic Javascript syntax coloring, auto-indentation, line numbers, creation of closing braces and quotes
- Context-sensitive help
- Code editing functions
- Syntax Editor Macros for typing commonly used code
- Syntax error checking
What color does blue represent in the Syntax Editor?
Reserved words
What do Bold/Italics represent in the Syntax Editor
Context menu items
What does the color green represent in the Syntax Editor?
Comments
How does Syntax Editor help with braces and quotes?
- It automatically adds the closing quote, braces or parenthesis
- It highlights pairs of brackets/parentheses
What is the benefit of using single quotes in JavaScript?
Values within single quotes can be compared with equals, equals equals, numeric comparisons and more.
What functionalities does the Context-sensitive Help provide?
- Displays a list of valid elements at the cursor’s current position
- Lists methods for a class
- Lists expected parameters
How do you display valid elements at the cursor’s current position within the Syntax Editor?
CTRL + Spacebar at the beginning of a line
How do you view a list of methods for a class within the Syntax Editor?
Type a period after the class name
How do you view a list of expected parameters for a class or method?
Type open parenthesis after a valid class, function or method name.
If you create custom objects, does the Syntax Editor provide any suggestions for variables belonging to the object?
Yes, if you type a period after the object, it will suggest variables belonging to the object.
What options are presented when you right click on bold/italicized text within the Syntax Editor?
The context menu:
- Show definition
- Show data
- Find references
How do you find a list of other places an object is used from within Syntax Editor?
Right click on the context menu and click ‘Find references.’
What is an alternative method of adding commments to manually typing forward slashes?
- Highlight applicable text
- Click the “Toggle Comment” button in the bar at the top of the Syntax Editor.
True or false: “Replace all” asks for confirmation before making the replacement(s)?
False. It does not.
What contains shortcuts for commonly used code?
The Syntax Editor Macros
How do you insert a Syntax Editor macro into your code?
Type the macro name and press tab.
How do you find the full list of Syntax Editor macros within the editor?
Type “help” and press tab.
What can the Syntax Editor “Syntax Checker” find?
- Missing characters, such as { and [
- Missing ; at the end of statements
- Incomplete arguments in for loops
- Bad function calls
What is the Syntax Editor “Syntax Checker” not able to find?
Typos, in:
- variable names
- function calls
- method calls
What do red circles indicate in Syntax Editor? Orange?
- Red: Errors
- Orange: Warnings
What type of script presents an error at the bottom of the script if you attempt to save it without resolving an error?
Server-side
How do you view all configuration changes made to a table?
Type the table name in the Filter Navigator and append with .config
What are client scripts used for?
Provide 7 examples.
Managing behavior of forms, fields and lists in real time
- Make fields mandatory
- Set one field in response to another
- Modify choice list options
- Hide/show form sections
- Hide fields
- Display alerts
- Manage/prohibit list editing
When is an “onLoad” client script run?
On load BEFORE control is given to the user.
When is the “onSubmit” client script run?
When the form is saved, submitted or updated (when a button is clicked)
What are “onSubmit” client scripts typically used for?
Field validation
What is the purpose of an “onChange” client script?
- Respond to field values of interest
- Modify one field value in response to another
When might you use the ‘isTemplate’ parameter?
When you want to prevent a client script from running when a template is to be applied so that values from the template aren’t overwitten.
In an onChange client script, what does oldValue represent and when does it change?
The date saved in the server.
Old value is not changed until it is saved back to the server again.
What is the purpose of the GlideForm API?
- Customizing the view of forms
- Managing form fields and their data
Which value, server or form, does g_form.getValue() retrieve?
Value selected on form
When g_form.getValue() is called against a string field, what is returned?
The string field as it appears on the form
When g_form.getValue() is called against a non-string field, what is returned?
The value as it is saved in the server, not the display/user-friendly label.
What functionality does the GlideUser API provide?
Methods to access information about the currently logged in user.
What scope does the GlideUser API belong to?
Global
Which debugging method is suggested for catching runtime errors?
Try/catch
How are views input on the client script record?
Comma separated list
In what order to client scripts run if they have the same value in the “order” field?
Either by created or the sys ID with the lower hexadecimal value.
What causes a GlideRecord query to execute the query?
gr.query();
How would you check if a GlideRecord query has another record?
if(myObj.hasNext()) {}
How do you add an OR condition to a GlideRecord query?
- Create the GlideRecord object
- Create a new variable and assign your addQuery to it
- Run .addOrQuery on the variable created in the step above
var gr = new GlideRecord(‘table’);
var q1 = gr.addQuery(‘cat’, ‘lucy’);
q1.addOrQuery(‘cat’, ‘felix’);
What would your query be if you wanted the following:
- State is 3 or State is 5
- AND priority is 1 or impact is 1
var gr = new GlideRecord(‘incident’);
var q1 = addQuery(‘state’, 3);
q1.addOrQuery(‘state’, 5);
var q2 = addQuery(‘priority’, 1);
q2.addOrQuery(‘impact’, 1);
When counting records, what is the benefit of using getRowCount?
What is the benefit of GlideAggregate?
- getRowCount: Less code but larger performance impact. Because it’s getting all the records/their details and then counting them.
- GlideAggregage: More code but executes quicker
How do you use getRowCount?
After .query(), assign gr.getRowCount() to a new variable.
How do you use GlideAggregate?
var count = new GlideAggregate(‘table’);
count.addQuery(‘field’, ‘value’);
count.addAggregate(‘COUNT’);
count.query();
var incidentCount = 0;
if (count.next()) {
incidents = count.getAggregate(‘COUNT’);
}
When is it recommended to use GlideAggregate?
If your table has more than 100 rows.
What are three benefits of GlideQuery?
- Presents errors as soon as possible (Fail fast)
- Behaves like normal Javascript, instead of mixing Java and Javascript elements like GlideRecord
- Do more with less lines of code
How do you write the following query in GlideQuery?
var gr = new GlideRecord(‘task’);
gr.addQuery(‘closed_date’, ‘<’ ‘2016-01-01’);
gr.query();
gr.deleteMultiple();
new GlideQuery(‘task’)
.where(‘closed_date’, ‘<’ ‘2016-01-01 00:00:00’)
.deleteMultiple();
*Note how there are no semicolons until the last statement.
The following query’s field is mistyped. What happens when it is executed?
var gr = new GlideRecord(‘task’);
gr.addQuery(‘closed_date’, ‘<’ ‘2016-01-01’);
gr.query();
gr.deleteMultiple();
The addQuery is ignored.
All task records are deleted and no error is generated.
The following query’s field is mistyped. What happens when it is executed?
new GlideQuery(‘task’)
.where(‘closed_date’, ‘<’ ‘2016-01-01 00:00:00’)
.deleteMultiple();
The NiceError shows the problem and known fields
How do you write this query in GlideQuery?
var gr = new GlideRecord(‘task’);
gr.addQuery(‘approval’, ‘not requested’);
gr.query();
while (gr.next()) {
doSomething(gr.assigned_to,gr.description);
}
new GlideQuery(‘task’)
.where(‘approval’, ‘not requested’)
.select(‘assigned_to’, ‘description’)
.forEach(doSomething);
In the code below, ‘not_requested’ is not a valid choice. What happens when it is executed?
var gr = new GlideRecord(‘task’);
gr.addQuery(‘approval’, ‘not_requested’);
gr.query();
while (gr.next()) {
doSomething(gr.assigned_to,gr.description);
}
The script runs, but no results are returned.
In the code below, ‘not_requested’ is not a valid choice. What happens when it is executed?
new GlideQuery(‘task’)
.where(‘approval’, ‘not_requested’)
.select(‘assigned_to’, ‘description’)
.forEach(doSomething);
The NiceError shows the invalid choice and allowed values.
The priority value used in the code below is invalid. What happens when it is executed?
new GlideQuery(‘task’)
.where(‘priority’, ‘<’, ‘v’)
.whereNotNull(‘assigned_to’)
.select(‘assigned_to.name’, ‘priority’)
.forEach(doSomething);
The NiceError shows the type error and the expected type.
How do you check the type of a value?
typeof(whatyourechecking)
Which values are falsy?
All values are truthy unless they are defined as falsy:
* false
* 0
* -0
* 0n
* “”
* null
* undefined
* NaN
How do you create a record with the following attributes using GlideQuery on the sys_user table?
- Username: sock.monkey
- First name: Sock
- Last name: Monkey
- Roles: admin
new GlideQuery(‘sys_user’)
.insert({
user_name: ‘sock.monkey’,
first_name: ‘Sock’,
last_name: ‘Monkey’,
roles: ‘admin’
})
.get()
How do you retrieve roles from a record on the sys_user table with the username of sock.monkey using GlideQuery?
var newUser = new GlideQuery(‘sys_user’)
.where(‘user_name’, ‘sock.monkey’)
.selectOne(‘roles’)
.get();