Syntax editor

The syntax editor provides support for editing JavaScript scripts.

The syntax editor has these features.

  • JavaScript syntax coloring, indentation, line numbers, and automatic creation of closing braces and quotes
  • JavaScript support
  • Script macros for common code shortcuts

This feature requires the Syntax editor plugin.

Figure 1. JavaScript editor

Script macro maintenance

Administrators can define new script macros or modify existing script macros.

Before you begin

Role required: admin

About this task

Script macros provide shortcuts for typing commonly used code. Several script macros are available by default. Administrators can define new or modify existing script macros.

Procedure

  1. Navigate to System Definition > Syntax Editor Macros.
  2. Click New or select the macro to edit.
  3. Define the macro details with the fields listed in the table below.
    Table 1. Editor macro fields
    Field Description
    Name Macro keyword text users type to insert macro text.
    Comments Description of the macro. This text appears when the user types help.
    Text Full macro text that replaces the name in the editor.

Syntax editor plugin

Syntax editor plugin enables use of the syntax editor.

The syntax editor enables the following features for all script fields:

  • JavaScript syntax coloring, indentation, line numbers, and automatic creation of closing braces and quotes
  • Code editing functions
  • Code syntax checking
  • Script macros for common code shortcuts

Figure 2. JavaScript syntax editor

The syntax editor can be disable/enable by modifying the glide.ui.javascript_editor property in the sys_properties.list. In addition, administrators can configure the syntax editor to show error and warning indicators next to a line of code that contains an error by modifying the glide.ui.syntax_editor.show_warnings_errors property. For information on the sys_properties.list, refer to Available system properties.

Note: Administrators can disable or enable the syntax editor for all users, regardless of user preference.

Searching for errors by line

To locate the exact position of the error in a large script, click the Go to line icon.

This feature is particularly useful when you are encounter a syntax error in a log file rather than in the ServiceNow record itself. In this case, you can navigate to the record and search for errors by line number. In the dialog box that appears, enter the line number of an error, and then click OK. Your view moves to the site of the error, and the cursor marks the correct line and column.

Note: For this feature to function, you must disable the Syntax Editor.
Figure 3. Go to script error

Navigate to a line number

When the syntax editor is disabled, users can navigate to a specific line in the code using the Go to line icon (The go to line icon).

  1. Click the the Go to line icon (The go to line icon). A text field appears.
    Note: This icon is not available when the editor is enabled.
  2. Enter a number in the field and then press Enter.

Syntax editor JavaScript support

The syntax editor provides editing functions to support editing JavaScript scripts.

JavaScript editing functions

Icon Keyboard Shortcut Name Description
N/A Toggle Syntax Editor Disables the syntax editor. Click the button again to enable the syntax editor.
Access Key + R Format Code Applies the proper indentation to the script.
Access Key + C Comment Selected Code Comments out the selected code.
Access Key + U Uncomment Selected Code Removes comment codes from the selected code.
N/A Check Syntax Checks the code for syntax errors. By default, the system automatically checks for syntax errors as you type in a script field. If an error or warning is found, the syntax editor displays a bullet beside the script line containing the error or warning. This check occurs on all script fields.
Access Key + \ Start Searching Highlights all occurrences of a search term in the script field and locates the first occurrence. Click the icon, then enter the search term and press Enter. You can use regular expressions enclosed in slashes to define the search term. For example, the term /a{3}/ locates aaa.
Access Key + [ Find Next Locates the next occurrence of the current search term in the script field. Use Start Searching to change the current search term.
Access Key + ] Find Previous Locates the previous occurrence of the current search term in the script field. Use Start Searching to change the current search term.
Access Key + W Replace Replaces the next occurrence of a text string in the script field.
  1. Click the icon, then enter the string to replace and press Enter. You can use regular expressions enclosed in slashes to define the string to replace. For example, the term /a{3}/ locates aaa.
  2. Enter the replacement string and press Enter.
Access Key + ; Replace All Replaces all occurrences of a text string in the script field.
  1. Click the icon, then enter the string to replace and press Enter. You can use regular expressions enclosed in slashes to define the string to replace. For example, the term /a{3}/ locates aaa.
  2. Enter the replacement string and press Enter.
N/A Save Saves changes without leaving the current view. Use this button in full screen mode to save without returning to standard form view.
Access Key + L Toggle Full Screen Mode Expands the script field to use the full form view for easier editing. Click the button again to return to standard form view. This feature is not available for Internet Explorer.
Access Key + P Help Displays the keyboard shortcuts help screen.

JavaScript editing tips

  • To fold a code block, click the minus sign beside the first line of the block. The minus sign only appears beside blocks that can be folded. To unfold the code block, click the plus sign.
  • To insert a fixed space anywhere in your code, press Tab.
  • To indent a single line of code, click in the leading white space of the line and then press Tab.
  • To indent one or more lines of code, select the code and then press Tab. To decrease the indentation, press Shift + Tab.
  • To remove one tab from the start of a line of code, click in the line and press Shift + Tab.

JavaScript resources

Scripts use ECMA 262 standard JavaScript. Helpful resources include:

Syntax editor macros

Script macros provide shortcuts for typing commonly used code. To insert macro text into a script field, enter the macro keyword followed by the Tab.

vargr
  • Description: Inserts a standard GlideRecord query for a single value.
  • Output:
    var gr = new GlideRecord("");
    gr.addQuery("name", "value");
    gr.query();
    if (gr.next()) {
    
    }
                  
vargror
  • Description: Inserts a GlideRecord query for two values with an OR condition.
  • Output:
    var gr = new GlideRecord('');
    
    var qc = gr.addQuery('field', 'value1');
     
    qc.addOrCondition('field', 'value2');
    gr.query();
                    
    while (gr.next()) {
    
    }
                  
for
  • Description: Inserts a standard recursive loop with an array.
  • Output:
    for (var i=0; i< myArray.length; i++) {
     //myArray[i];
    
    }
info
  • Description: Inserts a GlideSystem information message.
  • Output:
    gs.addInfoMessage("");
method
  • Description: Inserts a blank JavaScript function template.
  • Output:
    /*_________________________________________________________________
       * Description:
       * Parameters:
       * Returns:
       ________________________________________________________________*/
       : function() { 
    
       },
    
doc
  • Description: Inserts a comment block for describing a function or parameters.
  • Output:
    /**
    
    * Description:
             
    * Parameters: 
      
    * Returns:
    */

Script syntax error checking

All script fields provide controls for checking the syntax for errors and for locating the error easily when one occurs. The script editor places the cursor at the site of a syntax error and lets you search for errors in scripts by line number.

Figure 4. Script syntax check

The script editor notifies you of syntax errors in your scripts in the following situations.

  • Save a new record or update an existing record. A banner appears at the bottom of the editor showing the location of the first error (line number and column number), and the cursor appears at the site of the error. Warnings presented at Save or Update show only one error at a time.
    Figure 5. Script syntax error (short)
  • Click the syntax checking icon before saving or updating a record. A banner appears at the bottom of the editor showing the location of all errors in the script, and the cursor appears at the site of the first error.
    Figure 6. Script syntax error

Syntax editor keyboard shortcuts and actions

The syntax editor offers keyboard shortcuts and actions to assist in writing code.

Table 2. Syntax editor keyboard shortcuts and actions for writing code
Keyboard shortcut or action Description Example
Write code

Scripting assistance

Control+Spacebar

Displays a list of valid elements at the insertion point such as:
  • Class names
  • Function names
  • Object names
  • Variable names

Double-click an entry to add it to the script.

A line of code for a variable definition with a pop-up window displaying potential scripting elements.
Enter a period character after a valid class name.

The system displays a list methods for the class.

Double-click an entry to add it to the script.

A line of code for a variable definition with a pop-up window displaying potential scripting elements.
Enter an open parenthesis character after a valid class, function, or method name.

The system displays the expected parameters for the class or method.

Enter the expected parameters as needed.

A line of code for a variable defining a method call. There is a pop-up window displaying potential scripting elements.

Toggle full screen mode

Control+M

Switches between displaying the form with the full screen and displaying it normally.
Format code
  • Windows: Control+Shift+B
  • Mac: Command+Shift+B
Formats the selected lines to improve readability.
Several lines of unformatted code
Several lines of code formatted for readability
Toggle comment
  • Windows: Control+/
  • Mac: Command+/
Adds or removes the comment characters // from the selected lines.
A line of code selected with no comment characters
A line of code selected with comment characters added to the front of the line.
Insert macro text
  1. In the Script field, type the macro keyword text. For example help.
  2. Press Tab.
Inserts macro text at the current position.
A line of code containing the string help.
The macro text for help added to the script.
Search
Start search
  • Windows: Control+F
  • Mac: Command+F

Highlights all occurrences of a search term in the script field and locates the first occurrence.

You can create regular expressions by enclosing the search terms between slash characters . For example, the search term /a{3}/ locates the string aaa.

A search field with the term gr.
The search results of the search term gr displaying four highlighted items. The first search result is selected.
Find next
  • Windows: Control+G
  • Mac: Command+G
Locates the next occurrence of the current search term in the script field. Use Start Searching to change the current search term.
The search results of the search term gr displaying four highlighted items. The second search result is selected.
Find previous
  • Windows: Control+Shift+G
  • Mac: Command+Shift+G
Locates the previous occurrence of the current search term in the script field. Use Start Searching to change the current search term.
The search results of the search term gr displaying four highlighted items. The first search result is selected.
Replace
  • Windows: Control+E
  • Mac: Command+E
Replaces the next occurrence of a text string in the script field.
A replace field with the string gr.
A replace with field with the string gl.
A replace confirmation dialog with options for Yes, No, and Stop
Replace all
  • Windows: Control+;
  • Mac: Command+;
Replaces all occurrences of a text string in the script field.
A replace field with the search term gr.
A replace with field with the string gl.
Help
Help
  • Windows: Control+H
  • Mac: Command+H
Displays the list of syntax editor keyboard shortcuts.
A pop-up window displaying the keyboard shortcuts for the Syntax Editor.
Show description
  • Windows: Control+J
  • Mac: Command+J
Display API documentation for the scripting element at the cursor's current location.
A line of code selected with a pop-up window displaying API help for the GlideRecord object
Show macros
  1. In the Script field, type help.
  2. Press Tab.
Displays the list of available syntax editor macros as text within the script field.
A line of code containing the string help.
The macro text for help added to the script.