Skip to content
Jared Healy edited this page Jul 20, 2019 · 3 revisions

ServiceNow Coding Standards

These standards define the practices that developers should attempt to adhere to. The main goal of these standards is consistency, readability, and supportability.

General Scripting

Script Varaible, Function, and Class Naming Standard.

Guiding Principles

  • Prefer spaces between elements over compact no-spaces formats
  • Prefer line breaks over single line compact
  • Comments: More is better

Variables

  • Name should be camelCase
  • Variables names should be descriptive over short
  • Acronymns in variable names should be all caps, including ID
    • Examples: userID, serverCIThing
  • Variable type prefixing / Hungarian notation is required for the following
    • GlideRecord: grServer, grTask, grUser

Functions

  • Utilize JSDoc fromatting for function descriptions:
  • Function names should be camelCase
  • Function arguments should be separated by a single space
  • Opening curly bracket should follow on the same line as the function declaration one space after the arguments
  • Indentation should follow the "auto-formatter" functions in the specific part of ServiceNow. The "select-all" and "shift-tab" trigger the auto-formatting and are the most consistent.
    • Back end scripts use tabs but the portal uses 2 spaces
  • Specific format
    /**
     * Summary description of what this thing does. Manually break lines around
     * the 80 character length. This enhances the readability.
     * 
     * @param {string} arg1 The first argument that is required
     * @param {string} arg2 The second parameter that is required
     * @return {string} The joined result of the two arguments
     */
    myFunction(arg1, arg2) {
        // Arg2 is optional, set default value
        arg2 = ar2 || "";
    
        // Calculate the result
        var result = arg1 + arg2;
    
        // Return the result
        return result;
    }

Other Code Block Types (if/else, for loop)

  • If, Else If, Else
    • Similar to functions, the open bracket should occur on the same line as the if with one space separating it from the argument parenthesis.
    • "Else", and "else if" should occur on the next line and follow the same format as an independent "if".
      var state = current.getValue('state') * 1;
      if (state === 15) {
          // True
      }
      else if (b === c) {
          // False
      }
      else {
          // Last
      }
  • For, While and other loops
    • Prefer spacing between parameters for readability
      for (var i = 0; i < servers.length; i++) {
      
      }
  • IFFE (Immediately Invoked Function Expression) Format
    • These are Javascript functions that execute as soon as they are defined. ServiceNow has begun using these throughout the system to clean up separate function call and definitions in things like business rules and client scripts.
    • Place a line break after the function declaration and the internal code and another line break after the code but before the closing brackets and parenthesis.
      (function(){
      
          // Line break before and after internal code
      
      })();
  • Object Definition
    • Place the opening bracket on the same line as the object name.
    • Use colon plus one space between each property and its value.
    • Use quotes around string values, not around numeric values.
    • Do not add a comma after the last property-value pair.
    • Place the closing bracket on a new line, without leading spaces.
    • Always end an object definition with a semicolon
      var test = {
          firstKey: "test1",
          secondKey: "test2",
      };

Specific ServiceNow Class/Object Types

The below sections cover specific artifacts that may have unique naming and or scripting practices tied to them.

Script Includes

When creating script includes, this covers naming, usage and other concerns

Private Functions

  • Should be prefixed with an underscore
  • Should be grouped at the end of the script include
    var u_ExampleClassInclude = Class.create();
    u_ExampleClassInclude.prototype = {
        initialize: function(userId) {
            // Global variables can be defined here
            this.grUser;
            this.hasUser = false;
            if (typeof userId !== 'undefined') {
                this.grUser = new GlideRecord('sys_user');
                this.hasUser = this.grUser.get(userId);
            }
        },
    
        /*
         * This funciton is used to return a greeting to the specified user. The
         * argument can be set when initialized or by passing to the function.
         *
         * @param: {string}  name The name you would like to greet
         * @return: {string} Phrase compiled from the input name
         */
        function runMainLogic(name) {
            // Handle optional name
            name = name || "Unknown";
            if (name === "Unknown" && this.hasUser) {
                name = this.grUser.getDisplayValue();
            }
    
            // Primary logic function
            var greeting = this._myPrivateFunction("My Name Is ", name);
    
            // Return the function result
            return greeting;
        },
    
        /*
         * Internal private function to construct our greeting.
         * 
         * @param: {string} name The name you would like to greet
         * @return: {string} Phrase compiled from the input name
         */
        function _myPrivateFunction(phrase, name) {
            // Construct our greeting
            var greeting = result1 + name + "!!!";
    
            // Return the greeting
            return greeting;
        },
    
        type: 'u_ExampleClassInclude'
    };
  • Script Include Naming
    • Inlcudes with multiple class functions should use proper case:
      • u_ChangeRequestMadness
    • Includes with "global functions" and a single purpose should follow camel case:
      • u_getMyChangePriority
      • u_setDefaultValue
    • Class based includes are preferable in most cases so that they could be expanded. Only use "global function" includes if the function is very simple or there are other considerations forcing this route.

Business Rules

UI Scripts

UI Pages

UI Macros

Catalog Items

Portal Widgets

Notification Scripts

Workflow & Runscripts

Transform Maps