Coding a class

Let's write some code to see how this works.

  1. Create a new Script Include by navigating to System Definition > Script Includes and clicking on New. Fill out the fields, and Save.
    • Name: SimpleAdd
    • Script:
var SimpleAdd = Class.create(); 
SimpleAdd.prototype = { 
  initialize: function (n) { 
    gs.info('Creating new object'); 
    this.number = (n - 0) || 0;
  }, 
 
  increment: function () { 
    this.number++; 
    gs.info(this.number); 
    return this; 
  }, 
 
  type: 'SimpleAdd' 
}; 

The first line creates a variable called SimpleAdd and executes the Class.create function. This starts the definition of the object.

The second line is where the prototype is edited in order to make the class actually do something. The prototype consists of two functions: initialize ...

Get ServiceNow: Building Powerful Workflows now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.