Blog

Processors

Processors provide a customizable URL endpoint that can execute arbitrary server-side JavaScript code and produce output such as TEXT, JSON, or HTML.

Typically, you create processors when you want a URL query to:

  • Perform non-standard record operations.

  • Contain complex logic as part of the API.

  • Act on multiple tables.

  • Create a logical API that can abstract implementation details.

ServiceNow Documentation on Processors

Examples

Hello World

  1. Navigate to System Definition > Processors.

  2. Click New.

  3. Enter the following information.

Name: Hello
Type: Script
Path: Hello
Script:

(function process(g_request, g_response, g_processor) {
var name= g_request.getParameter("name");
g_processor.writeOutput("text/plain","Hello "+name);
})(g_request, g_response, g_processor);


Output

Substitute instance with your ServiceNow instance

https://instance.service-now.com/Hello.do?name=world

Hello world

Find SysId

Name: FindSysID
Type: script
Parameters: sysID
Path: FindSysID

Script:

(function process(g_request, g_response, g_processor) {
var sysID = g_request.getParameter("sysID");
var grObject = new GlideRecord('sys_db_object');
grObject.addEncodedQuery('nameNOT LIKEts_^sys_update_nameISNOTEMPTY^nameISNOTEMPTY^name!=item_option_new_backup');
grObject.query();
while (grObject.next()) {
var tableName = grObject.getValue('name');
grTable = new GlideRecord(tableName);
grTable.addQuery('sys_id',sysID);
grTable.query();
if (grTable.next()) {
g_processor.writeOutput(gs.getProperty('glide.servlet.uri') + tableName + '.do?sys_id=' + sysID+'\n')
}
}
})(g_request, g_response, g_processor);

Output


https://instance.service-now.com/FindSysID.do?sysID=965c9e5347c12200e0ef563dbb9a7156

Table ROW COUNTS

This one is amazing fast for all that it does.

Name: TableRowCounts
Type: script
Parameters:
Path: TableRowCounts

Script:

(function process(g_request, g_response, g_processor) {
	g_processor.writeOutput('Table | Count\n');
	var grDictionary = new GlideRecord('sys_dictionary');
	grDictionary.addQuery('internal_type', 'collection');
	grDictionary.addQuery('name','!=','sys_template');
	grDictionary.orderBy('name');
	grDictionary.query();
	while (grDictionary.next()) {
		var currentTable = grDictionary.name.toString();
		var count = new GlideAggregate(currentTable);
		count.addAggregate('COUNT');
		count.query();
		if (count.next()) {
			g_processor.writeOutput(currentTable+ ' | '+ count.getAggregate('COUNT') + '\n');
		}
	}
})(g_request, g_response, g_processor);

Output


https://instance.service-now.com/TableRowCounts.do

FindUnique

Parameters: table,field

Path: FindUnique

Script:

(function process(g_request, g_response, g_processor) {
	var table = g_request.getParameter("table");
	var field = g_request.getParameter("field");
	var au = new ArrayUtil();
	var uniqueArray = [];
	var grTable = new GlideRecord(table);
	grTable.orderBy(field);
	grTable.addNotNullQuery(field);
	grTable.query();
	while (grTable.next()) 
		
	
	g_processor.writeOutput('Unique Values: ' +au.unique(uniqueArray));
})(g_request, g_response, g_processor);

Output

https://instance.service-now.com/FindUnique.do?table=cmdb_ci_computer&field=osUnique

AIX,HP/UX,Linux Red Hat,Mac OS 10 (OS/X),OS/400,Solaris,Windows 2000,Windows 2000 Advanced Server,Windows 2000 Datacenter Server,Windows 2000 Professional,Windows 2000 Server,Windows 2003 Standard,Windows 95,Windows NT 4.0,Windows XP,Windows XP Professional

Table Size

Name: TableSize
Type: script
Description: Return number of records in a table
Parameters: SIZE
Path:

Script

(function process(g_request, g_response, g_processor) {
	g_response.setContentType('text/html;charset=UTF-8');
	if(g_target === 'sys_email' || g_target === 'sys_log' )
	{
		g_processor.writeOutput(g_target + ' table is too large to quickly count');
	} else {
		var count = new GlideAggregate(g_target);
		if( count.canRead() ) {
			count.addAggregate('COUNT');
			count.query();
			var records = 0;
			if (count.next()) {
				records = count.getAggregate('COUNT');
			}
			g_processor.writeOutput('table ' + g_target + ' has ' + records + ' records');
		} else {
			g_processor.writeOutput('You do not have access to table ' + g_target);
		}
	}
})(g_request, g_response, g_processor);

Output

https://<instancename>.service-now.com/incident.do?SIZE

https://<instancename>.service-now.com/sys_user.do?SIZE

Your instance reports the number of records in the table. For example, table incident has 82 records.