Syntax Editor Macros in ServiceNow

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 1

Secret ServiceNow Macro

There is a hidden macro in ServiceNow’s script editor called “help”, which fetches a list of available Script Macros from the “syntax_editor_macro” table and displays their name and comment in the script editor, allowing users to quickly reference and use the out-of-the-box macros.

Custom Syntax Editor Macros

Creating custom macros in ServiceNow can greatly improve productivity and efficiency by automating repetitive or complex tasks that are unique to a particular development process or team. One use case for custom macros could be for documentation purposes, where a macro can be created to insert a standardized comment block at the beginning of a script file, reducing the time and effort required to write consistent documentation. Additionally, custom macros can help enforce coding standards and best practices, ensure consistency across the development team, and reduce the likelihood of errors or omissions in code documentation.

The Bad (UPDATE)

The custom macros created in ServiceNow won’t be fetched or displayed by the “help” macro, as it only retrieves out-of-the-box macros from the “syntax_editor_macro” table.
The OOB macro ‘help’ now lists all Syntax Editor Macros including custom ones.

The Good

I created a business rule in ServiceNow that updates a custom macro called “list”, which dynamically adds all out-of-the-box macros and any custom macros created manually to the list, allowing for a comprehensive overview of all available macros.

Steps to Reproduce

Create a new Syntax Editor Macro

  1. In ServiceNow, go to “System Definition” > “Syntax Editor Macros”.
  2. Click on “New” to create a new macro.
  3. Enter “list” as the name for the macro.
  4. Enter a comment as you wish
  5. Click “Submit” to save the macro

Create a new Business Rule

  1. In ServiceNow, go to “Business Rules” and click “New”.
  2. Enter “Update Syntax Editor Macro - List” as the name for the business rule.
  3. For the “Table” field, select “syntax_editor_macro”.
  4. For the “When to run” field, select “After” and check the boxes for “Insert” and “Update”.
  5. For the “Order” field, enter “250”.
  6. For the “Filter conditions” field, enter “name” “is not” “list”.
    • Info: it is not best practice to work with names in ServiceNow, but instead use Sys IDs
  7. Set the “Advanced” checkbox to true
  8. In the “Advanced” tab, paste the following code in the “Script” field:
(function executeRule(current, previous /*null when async*/ ) {
	
    // query the "list" macro which will be updated
    var grSyntaxMacroList = new GlideRecord('syntax_editor_macro');
    grSyntaxMacroList.addQuery('name', 'list');
    grSyntaxMacroList.query();
    
    if (grSyntaxMacroList.next()) {
		// empty the text so we can fill with base text and new information
        grSyntaxMacroList.text = '';
        grSyntaxMacroList.text += 'The complete list of Syntax Editor Macros is: ' + 
            '\n' + '-----------------------------' + '\n';

        // collect the data from all macros to be saved in the list text
        var grSyntaxEditorMacro = new GlideRecord('syntax_editor_macro');
        grSyntaxEditorMacro.addQuery('name', 'NOT IN', 'list');
        grSyntaxEditorMacro.orderBy('sys_created_on');
        grSyntaxEditorMacro.query();
        
        while (grSyntaxEditorMacro.next()) {
            grSyntaxMacroList.text += grSyntaxEditorMacro.name + 
                ' - ' + grSyntaxEditorMacro.comments 
                + '\n';
        }
    grSyntaxMacroList.update();
    }

})(current, previous);

Test the Script and the custom Syntax Editor Macro ’list’