THURSDAY, JANUARY 19, 2017

Generate a GlideRecord Query for a List

Have you ever wanted to 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 ran into this yesterday when I was working on a scripted web service with about 50 inputs and was making a second related web service that needed the same inputs as the first. I ended up writing a background script to do a simple query of the first web service inputs and insert a copy of each one for the new web service.

Using this little helper I put together the other day saved me some time and hassle of tracking down the table names and filters to get the right query. It’s a simple little script that makes life as an admin just a little bit easier.

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.

4 Comments

Jack Scotty 14-08-2014, 14:47

Any reason why you are using ‘addQuery()’ instead of ‘addEncodedQuery()’ in the alert message?

Reply
James Farrer 14-08-2014, 15:11

Only because that’s what came to my mind first. ‘addQuery()’ can take 1, 2, or 3 parameters. If you only pass it one, it treats it as an encoded query behaving the same as ‘addEncodedQuery()’.

Reply
Alain Müller (Aspediens) 15-08-2014, 02:43

Hello James,

Thanks for sharing this!
Two remarks:
1. Your code is using the “g_form” object which, I believe, is not available from a List.
2. We have just discovered at Aspediens that the object “g_list” seems to be null in Eureka. We have opened an Incident in /hi for this issue.

Kind Regards,

Alain

Reply
James Farrer 15-08-2014, 09:11

I put the checking in place to make sure g_form is there and handle it gracefully if it’s not. When the list is a Related List g_form is available and is used to build the query that includes the current record.

Thanks for the heads up on it not working in Eureka. There’s nothing in the documentation about it so I’ll have to keep an eye on it.

Reply

Leave a Reply


Latest Comments

  • David: It appears that I can hit sys_properties table with REST. This works, but I haven’t yet discovered the...
  • Mark Stanger: Hey David, It doesn’t surprise me that scoped apps have made this more difficult. I’m not...
  • David: Mark, do you have an example of how to do this in a scoped app? It seems there are many hoops to jump through...
  • Mark Stanger: The only possibility is to create a system property to override this in your application. Check out the...