Please use docs.servicenow.com for the latest documentation.

This site is for reference purposes only and may not be accurate for the latest ServiceNow version

Using the Syntax Editor

From Wiki Archive
Jump to: navigation, search
Note
Note: This article applies to Fuji and earlier releases. For more current information, see Syntax Editor at http://docs.servicenow.com

The ServiceNow Wiki is no longer being updated. Visit http://docs.servicenow.com for the latest product documentation.


{{Topic|Scripting Glide and Jelly

Overview

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

This feature requires the Syntax Editor plugin.

JavaScript syntax editor

Enabling the Syntax Editor

You can choose whether to use the syntax editor. This choice is stored as a user preference, and the editor opens in the last mode selected the next time you access a script field. To disable or enable the syntax editor, click the Toggle syntax editor (JSenable.png) icon.

The syntax editor is enabled when the Toggle syntax editor icon and the code appear in color. The code editing tools are available in this mode only.

Syntax editor is enabled

The syntax editor is disabled when the Toggle syntax editor icon appears in gray and the code appears in black. The Go to line icon is available in this mode only.

Syntax editor is disabled

Navigating to a Line Number

To navigate to a specific line in the code when the syntax editor is disabled:

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

Editing JavaScript with the Syntax Editor

The following table lists the JavaScript editing functions that are available on the syntax editor toolbar.

Icon Keyboard Shortcut Name Description
JSenable.png N/A Toggle Syntax Editor Disables the syntax editor. Click the button again to enable the syntax editor. See Enabling the Syntax Editor.
File:Format code.gifx Access Key + R Format Code Applies the proper indentation to the script.
Icon-comment.png Access Key + C Comment Selected Code Comments out the selected code.
JSuncomment.png Access Key + U Uncomment Selected Code Removes comment codes from the selected code.
File:Js validate.gifx N/A Check Syntax Checks the code for syntax errors. See Checking Code Syntax.
File:Find.gifx 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.
File:Find next.gifx 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.
File:Find previous.gifx 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.
File:Replace.gifx 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 the replacement string and press Enter.
File:Replace all.gifx 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 the replacement string and press Enter.
JSSave.png 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.
File:Full screen.gifx 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.
File:Help.gifx Access Key + P Help Displays the keyboard shortcuts help screen.

Additional 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 whitespace 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.

Checking Code Syntax

To check for syntax errors in a script field, click the Check syntax (File:Js validate.gifx) icon. Error details appear below the script field. To learn more, see Script Syntax Error Checking.

The syntax editor also provides real-time error (JSerror.png) and warning (JSwarning.png) indicators beside a line of code that contains a syntax error. To view the error or warning details, point to the indicator beside the line number.

Note
Note:
  • Administrators must configure real-time syntax checking to show errors only or both error and warnings.
  • The system checks for syntax errors when you save a record. If an error is found, details appear beneath the field and the record is not saved. This check occurs on all script fields and cannot be disabled.


Using Script Macros

Script macros provide shortcuts for typing commonly used code. To use a macro, type the macro name in a script field and press Tab. The macro name is replaced with the full macro text.

The following macros are available by default. Administrators can define additional script macros.

To view a list of the macros that are available in your system, type help in a script field and press Tab.

Name Description Code text
vargror GlideRecord query with OR condition <source lang="javascript">

var gr = new GlideRecord('$0'); var qc = gr.addQuery('field', 'value1'); qc.addOrCondition('field', 'value2'); gr.query(); while (gr.next()) {

} </source>

vargr GlideRecord query example <source lang="javascript">

var gr = new GlideRecord("$0"); gr.addQuery("name", "value"); gr.query(); if (gr.next()) {

} </source>

method Standard JavaScript class method <source lang="javascript">

/*_________________________________________________________________

  * Description:
  * Parameters:
  * Returns:
  ________________________________________________________________*/
  $0: function() {
  
  },

</source>

info Add an info message to the current session <source lang="javascript">

gs.addInfoMessage("$0"); </source>

for Standard loop for arrays <source lang="javascript">

for (var i=0; i< myArray.length; i++) { //myArray[i]; } </source>

doc Code documentation (comment) header <source lang="javascript">

/**

  • Description: $0
  • Parameters:
  • Returns:
  • /

</source>

JavaScript Resources

Scripts in ServiceNow use ECMA 262 standard JavaScript. Helpful resources include: