1

Syntax Editor & Macros in ServiceNow

The editor is a simple functionality that helps users enhance and make their work look better. In ServiceNow, writing scripts using a syntax editor is like working with a code editor. It helps developers write code in script fields. 

This topic focuses on the functionalities of a Syntax Editor and how macros save your time. 

What is a Syntax Editor?

ServiceNow Syntax Editor is a code editing tool/area available in script fields of business rules, Client Scripts, or Script Includes. It helps in creating a coding environment out of the plain text box. 

To work in the editor, always check syntax before saving scripts, as it catches missing brackets and typos. Also, keep the code formatted, as it helps with debugging and maintenance. 

ServiceNow Syntax Editor Functionalities

Before using the syntax editor, it is important to understand the functions it has to offer. Some of the major functions include: 

Function What it Does
Toggle Syntax Highlighting Turn on and off the colored syntax view.
Check Syntax Check code to identify errors and mark them.
Search / Replace Find and replace the text.
Jump to line Move directly to a line number.
Comment / Uncomment Add or remove comments on selected lines.
Expand / Collapse Editor Resize the editing area to make it large or small.
Help Display the editor’s shortcut list.

Remember, the buttons and keyboard shortcuts can be different based on releases or between the classic and new code editor. Let us move on to understanding these syntax editor shortcuts in ServiceNow. 

What are Syntax editor macros? 

Macros are shortcuts for code you type on a regular basis. These are keywords that you type in the script field, and the Tab key allows you to expand it into the full block of code. (Similarly to when you write common words in Google Docs.)

This is a great way to reduce errors in repetitive code and save time while working in the syntax editor. The macros are stored in the ServiceNow Syntax editor macros table so that admins can edit existing ones and add new ones by themselves.

Macro What it inserts
vargr A standard GlideRecord query for a single value.
vargror A GlideRecord query for two values joined by an OR condition.
for A standard loop that iterates over an array.
info A GlideSystem information message.
method A blank JavaScript function template.
doc A comment block for describing a function or its parameters.

How to create Syntax Editor shortcuts ServiceNow? 

To create or customize macros for a team’s or personal coding style, there are a few steps: 

  1. Move to System Definition in Syntax Editor Macros. 
  2. Open an existing macro to edit or click New to create one. 
  3. Add the name of the keyword you will type. 
  4. Add the text that will be inserted to the keyword. 
  5. Save the record and test it in the script field by typing the keyword and pressing Tab. 

Conclusion

ServiceNow Syntax editor helps developers highlight, check errors, search and make scripting faster and cleaner. It helps you verify the code using Check Syntax before you save and run it. 

By the end of this module, we have understood teh key functionalities, tools, and features used while scripting in ServiceNow. The next module focuses on fundamentals and a detailed guide to what JavaScripting is in ServiceNow. 

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.