Welcome Login
Blog Photos Links

RSS Feed

Generate a GlideRecord Query for a List

August 12, 2014, 8:55 am - James Farrer

Have you ever wanted to just get a quick GlideRecord Query for a list you're looking at? I've wanted to do that many times. Sometimes it's because I'm writing a business rule and sometimes I've got to run a background script to update some values. 

I finally took a couple minutes and put together a rather simple script that does just that. 

To set it up in your instance go to System UI -> UI Context Menus and open a new record. The other values should be as follows:

Table: Global

Menu: List Header

Type: Action

Name: Get GlideRecord Query

Condition: gs.hasRole('admin')

Order: 2000

Action script:

// Check for a query from the related list
var rel_list_prefix = '';
try{
	if(g_form != undefined){
		var field_name = g_list.getRelated().split('.')[1];
		if(field_name != undefined){
			var sys_id = g_form.getUniqueValue();
			rel_list_prefix = field_name + "=" + sys_id + "^";
		}
	}
} catch(e) {}

// Generate the query
var query = "var gr = new GlideRecord('" + g_list.getTableName() + "');\n";
query += "gr.addQuery('" + rel_list_prefix + g_list.getQuery() + "');\n";
query += "gr.query();\n";
query += "while(gr.next()) {\n";
query += "	\n";
query += "}";
alert(query);


Now that you've got this, from any List you can right-click on the header and the bottom option will be "Get GlideRecord Query" and you can copy the resulting code. It's nothing complicated, but can still save a bit of time.

The key to making this work is the g_list object that has the details for the list that you're on. It's documented fairly well on the ServiceNow Wiki and if you haven't seen it before I'd recommend glancing at what options are available.

Joel Olives

Great time saver James! Really resourceful thinking.

RS

There is also the ootb functionallity to right click on the breadcrumb/filter of a given list -> "Copy Query".

RS

Aaah, you also generate the code for the new query... ok forget the last comment :-)

Post a Comment

Name:

To reduce spam comments, please answer the following question:
What is three plus two?

What's New

There are currently no new items, please check back later.

Archives
2017 (1)
  January (1)
2016 (4)
  December (1)
  November (1)
  May (1)
  January (1)
2015 (1)
  December (1)
2014 (2)
  August (1)
  February (1)
2013 (4)
  October (1)
  July (1)
  June (1)
  April (1)
2012 (11)
  December (2)
  October (3)
  September (1)
  May (1)
  April (1)
  February (2)
  January (1)
2011 (14)
  December (1)
  November (1)
  September (2)
  July (2)
  June (1)
  May (1)
  April (2)
  March (3)
  January (1)
2009 (2)
  October (1)
  June (1)
2008 (1)
  September (1)