Utilizing the Script Editor - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Hi there,

 

Working with the Script Editor often, on Fix Scripts, Business Rules, Script Includes, Client Scripts, etcetera? And wondering about how to use it smarter, more efficient, etcetera? On the Developer site, there is already a nice basic explanation about the ServiceNow Script Editor. I will try to share some experiences gained from the field to utilize the Script Editor even better!

 

Note: This article is strictly about the ServiceNow Script Editor. If you prefer to use tools like VS Code, that's fine.

Out-of-the-box Script Editor

You all probably have seen the Script Editor before. It basically looks like:

 

find_real_file.png

 

Note, sometimes on Script fields, you don't see the Script Editor. For example, out-of-the-box, on Syntax Editor Macros and Count Gauges the Script field looks like:

 

find_real_file.png

 

Reason, the field type. If the field type is "Script" or "Script (plain)", then the Script Editor will be visible. If not, like below, just a Multi Line String field is displayed.

 

find_real_file.png

Toolbar

On top of the Script Editor the toolbar is displayed with almost all basic options. Hovering over the options, a Hint will be shown (with in most cases a Shortcut mentioned).

 

find_real_file.png

Shortcuts

Keyboard Shortcuts

Clicking the question mark icon (or Ctrl+H) will pop-up a dialog with most keyboard shortcuts:

 

find_real_file.png

 

Within the dialog, do note the bottom message about Macros. When typing "help" and hitting TAB, you will be presented with all available Syntax Macro Editors. Read more about Syntax Macro Editors in one of my previous articles: Expanding Syntax Editor Macros.

 

find_real_file.png

 

These shortcuts can also be combined. For example, if you would like to "Toggle Comment" on multiple lines, just select those lines and hit Ctrl+/.

 

find_real_file.png

 

find_real_file.png

 

Keyboard shortcuts not mentioned:
- Formatting your fullcode. Select your fullcode (Ctrl+A), and hit Ctrl+TAB.
- Saving the code record (or actually saving the record!). Ctrl+S.
- Obvious general shortcuts like Ctrl+C, Ctrl+P, Ctrl+X. Hope these don't have to be explained 😀

 

Mouse Shortcuts

Some mouse shortcuts that I find usefull:
- If you want to replace code (or add) on multiple lines, for example like below (gr), you could select all those lines with Alt+Left mouse click and dragging your mouse.

 

find_real_file.png

 

- If you would like to copy a table name, script name, etcetera. Instead of placing your mouse cursor in front of the name and dragging the name… simply double click on the name, the complete word will be selected!

 

find_real_file.png

Real-time Syntax Checking

The Script Editor indicates JavaScript syntax errors by placing warning and error indicators next to the line numbers. Hover over an indicator for more information.

 

find_real_file.png

 

find_real_file.png

 

Note: Real-time Syntax Checking applies only to JavaScript syntax.

Syntax Highlighting

The Script Editor applies color coding for readability.- Green: comments;
- Magenta: JavaScript objects;
- Blue: strings, reserved words.

 

The syntax highlighting can also assist you in spotting errors in your code. For example, below GlideRecord query. Note on line 8, "gr" is not highlighted blue. This indicates that gr is not declared… well actually it's an error in the script, it should have been grIncident.

 

find_real_file.png

Another example of syntax highlighting which can assist you in spotting errors in your code. On line 3, the table name "incident" is not highlighted. Actually this is because a string is expected here, and incident is missing quotes (or double quotes).

 

find_real_file.png

 

* New York * Last two examples of the syntax highlighting which can assist you in spotting errors in your code. Since New York, methods, tables, script includes are displayed italic and bold. So if your entered table name isn't valid (or you made a typo), the table name wouldn't be displayed italic and bold. If valid, the table name should look like:

 

find_real_file.png

 

(Also note that GlideRecord is displayed italic and bold here)

Scripting Assistance

The Script Editor contains some assistance in providing a contextual list of available properties and methods for script objects. The list appears when you are typing an object name like "current" followed by a period (.). On a new line you could also hit Ctrl+SPACE.

 

find_real_file.png

 

find_real_file.png

 

find_real_file.png

 

Notice that not only a contextual list is provided, though also guidance text in how to use the property of method.

 

Note: Not all available methods and properties are displayed in the contextual list. There are several undocumented methods and properties that will work fine, though are just not listed.

Show Documentation, Definitions and Data [New York]

New since the New York release is the highlighting of methods, tables and script includes in italic and bold. Though that's not all actually. Really nice addition is when you right-click on the method, table or script include. Options like "Show Documentation" (for methods), "Show definition" and "Show data" (for tables) and "Open definition" (for Script Includes) will be shown.

 

find_real_file.png

 

find_real_file.png

 

- Show Documentation: Opens the associated Docs page in a new browser tab;
- Show Definition: Opens the Tables record [sys_db_object] in a new browser tab;
- Show Data: Opens the Lists of records in a new browser tab;
- Open Definition: Opens the Script Include in a new browser tab.

 

These features will shorten your navigation search through the platform. Also all are opened in a new browser tab, so you are not losing the current window you are working on. A really nice addition in the New York release

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Consultant @ Quint Technology

---

LinkedIn

Comments
Markus Kraus
Kilo Sage

Great post!

Just a general tip for Syntax Highlighting:

1.) If this features seem not be working for, you might be missing the IIFE (Immediately Invoked Function Expression).
find_real_file.png
Script 1: Variable declarations and usages in- and outside of functions: Even though declared, the variable will not be highlighted properly if not inside a function.

Putting your code inside IIFE is best practice anyways.

2.) Linter settings (https://eslint.org/docs/rules/):
find_real_file.png
Script 2: With the proper linter settings in line 1+2, the "incidentGR" typo is now properly marked as an error.

Can be globally set via sys_properties.list "glide.ui.syntax_editor.linter.eslint_config". I would not recommend this however, as it will be applied system wide. Instead, i suggest doing it the way i have shown it in the example (Script 2).

EDIT: For Copy & Paste purposes:

/*global GlideRecord, current, event*/
/*eslint no-undef: "error"*/
Dr_ Patrik Bich
Tera Contributor

For Script Includes it may be helpful to add some more classes.

For example:

/*global GlideRecord, current, event, Class, GlideDateTime, GlideDate, gs */
/*eslint no-undef: "error"*/

Mark Roethof
Tera Patron
Tera Patron

Interesting comment, nice!

The first one should actually be no issue... if everyone is following the best practice of wrapping code in functions. Though it is indeed as you described.

The second one I do found really interesting! Haven't tested yet, though sounds like a really nice addition.

Kind regards,
Mark

Bert_c1
Tera Sage

Hi,

 

does anyone know how to remove the new script outline on the right side of the editor?

 

Screenshot 2024-05-19 154102.png

 

Seen once I upgraded to Washington DC release.

Version history
Last update:
‎08-03-2024 07:32 AM
Updated by:
Contributors