Learning to write Fix Scripts in ServiceNow – Part 1 – Query a Table

These posts will be detailing my experiences learning how to write Fix Scripts in ServiceNow. (We are currently in the Orlando release with Paris just around the corner.)

I want to start this out by expressing that I do not have a strong background in JavaScript. With that being said, I am learning JavaScript as well as ServiceNow methods and best practices. I thought since I was learning everything brand-new, I could share my experiences with others who may be going down a similar path.

I was recently assigned with quite the “first-time” scripting task. I had to identify any potential column, in any potential table, that had a certain record type associated to it. If I find that data, I need to change it to the new data type that matches the old one by name.

“OK” I thought to myself. Let’s think about this a little bit. We need to loop through every table, and look at every field, and look at the data in every field, then determine if that data is a specific class of data. YIKES! Especially because I don’t even really know how to do basic table queries in ServiceNow using javascript yet! So let’s begin there.

Query Table Data

So now we want to write our first script. I have a Discovery/CMDB background, so those are the tables I am going to begin with and play around in to start with. We are going to be creating a server-side script, because our script will be reviewing table data.

To begin, I went to the ServiceNow docs site here, which describes how to query tables in ServiceNow.

This gives us a nice start, and explains how to begin our script. We have to create a new GlideRecord. Great, let’s just go and…wait! What exactly is a GlideRecord, and why do I need one? If you are anything like me, and you absolutely need to know this before you can move forward, here are the details. Otherwise we are moving right along.

I want to query the ‘cmdb_ci_computer table’. So line 1 of my script will be: var recName = new GlideRecord('cmdb_ci_computer');
This line will create a new GlideRecord for the ‘cmdb_ci_computer’ table, and assign it to the variable called ‘recName’.

Now, if we just want to return all of the records in the table, we do not have to provide any query information. We can simply initiate the query, and manipulate our results. So let’s do that to begin.

var recName = new GlideRecord('cmdb_ci_computer');
recName.query();
while (recName.next()) {
gs.print(recName.name);
}

This small block of code will:

  1. Set the variable named ‘recName”.
    • Set the GlideRecord to point to the ‘cmdb_ci_computers’ table.
  2. Run the query.
  3. The next method will move to the next record in the query result.
  4. For every record that gets loaded, it will run the content of the While loop. (In this case it will print the data in a ServiceNow info window.)

This will conclude my first post on creating a Fix Script in ServiceNow. In the next post, we will modify the basic script block to include a query and return a more specific set of data.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s