Skip to main content

Writing Code in ServiceNow

Remember, my introduction to writing code in ServiceNow was from Scripting in ServiceNow that I took at Knowledge 13.

The most basic one being a GlideRecord to access data in a table from the server side.

// 1. Create an object to store rows from a specified table
var gr = new GlideRecord('table_name');

// 2. Build a query
gr.addQuery('field_name','operator','value');
gr.addQuery('field_name','operator','value');

// 3. Execute the query
gr.query();

// 4. Iterate through the returned record
while (gr.next()){
// Logic to execute.
}

Followed by GlideAjax to interface from the client to the server.

var ga = new GlideAjax('<name of Script Include>');
ga.addParam('sysparm_name', '<name of method in Script Include>'); // required
ga.addParam('sysparm_<parameter name>', <parameter value>); // optional
ga.getXML(<name of callback function to handle returned XML>); // executes call to server

function handleResultCallbackFunction (response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer); // display in alert dialog whatever was returned
}

We will explore both of these in much greater depth start now.