Zum Hauptteil springen

If you want to make code clean and readable, you should use meaningful variable names instead of literals. For example, the state of an Incident (incident) has the following internal values:

Incident States

To use these values in a Business Rule (sys_script) / Client Script (sys_script_client) or any other condition, you could write something like:

if(incident.getValue('state') == '1')  {
    // todo
}

 

While this will work it is not very readable especially not for other developers. You could leave a comment to explain what the literal "1" means, but that wouldn't be a viable option, when you are referencing the same value in different places. Our goal in this example is the following code:

if(incident.getValue('state') == IncidentState.NEW)  {
    // todo
}

 

The incident state is now represented by a variable with a meaningful name.
Have a look into the content of the Script Include (sys_script_include) "IncidentState":

var IncidentState = Class.create();

IncidentState.NEW                = IncidentStateSNC.NEW;
IncidentState.IN_PROGRESS        = IncidentStateSNC.IN_PROGRESS;
IncidentState.ACTIVE             = IncidentStateSNC.ACTIVE;
IncidentState.ON_HOLD            = IncidentStateSNC.ON_HOLD;
IncidentState.AWAITING_PROBLEM   = IncidentStateSNC.AWAITING_PROBLEM;
IncidentState.AWAITING_USER_INFO = IncidentStateSNC.AWAITING_USER_INFO;
IncidentState.AWAITING_EVIDENCE  = IncidentStateSNC.AWAITING_EVIDENCE;
IncidentState.RESOLVED           = IncidentStateSNC.RESOLVED;
IncidentState.CLOSED             = IncidentStateSNC.CLOSED;
IncidentState.CANCELED           = IncidentStateSNC.CANCELED;

IncidentState.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getIncidentState: function() {
		return new JSON().encode(IncidentState);
	},

    type: 'IncidentState'
});

 

See the code of a newly created ServiceNow developer instance. This is the structure which should be replicated for any other tables if they have a state field with internal number values. For example "ProblemState.RESOLVED", by simply replacing "IncidentState" with "ProblemState" and set the correct states plus values.

The values of "IncidentStateSNC" are defined in another Script Include (sys_script_include) with the same structure which is by default read-only because it is maintaned by ServiceNow.

var IncidentStateSNC = Class.create();

IncidentStateSNC.NEW                = "1";
IncidentStateSNC.IN_PROGRESS        = "2";
IncidentStateSNC.ACTIVE             = IncidentStateSNC.IN_PROGRESS;
IncidentStateSNC.ON_HOLD            = "3";
IncidentStateSNC.AWAITING_PROBLEM   = IncidentStateSNC.ON_HOLD;
IncidentStateSNC.AWAITING_USER_INFO = IncidentStateSNC.ON_HOLD;
IncidentStateSNC.AWAITING_EVIDENCE  = IncidentStateSNC.ON_HOLD;
IncidentStateSNC.RESOLVED           = "6";
IncidentStateSNC.CLOSED             = "7";
IncidentStateSNC.CANCELED           = "8";


IncidentStateSNC.prototype = {
    initialize: function() {
    },

    type: 'IncidentStateSNC'
};

 

Through the existence of this Script Include (sys_script_include) a descriptive code is already possible on the Service Side Code, but we want to enhance it to make it usable on Client Script (sys_script_client). Create a new Business Rule (sys_script) with the following values:

Load incident models into scratchpad

The script in the "Advanced" tab looks like this:

(function executeRule(current, previous /*null when async*/) {
	var _JSON = new JSON();	
	g_scratchpad.incidentState = _JSON.decode(_JSON.encode(IncidentState));	
})(current, previous);

 

The code takes the Script Include (sys_script_include) object, convert it into a JSON text and convert it further into a usable Java Script object. This approach makes it possible to use dot walking for a internal value of a state.

After that, a condition in a Client Script (sys_script_client) could look like this:

current.state == g_scratchpad.incidentState.NEW