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.

Client Side Dates in ServiceNow

February 18, 2014, 9:13 am - James Farrer

TL;DR

I keep coming back to this post for specific tidbits so I decided I would put the key code snippet right at the top to make it easy. Basically, to get a javascript date from a ServiceNow date field value do this:

var date_number = getDateFromFormat(g_form.getValue('the_date_field'), g_user_date_format);
var my_date = new Date(date_number);

Or, from a Date/Time field do this:

var date_number = getDateFromFormat(g_form.getValue('the_date_time_field'), g_user_date_time_format);
var my_date = new Date(date_number);

 

The Long Version

Working with dates on the client side of things in Service-Now has always been a challenge. Part of this is just due to Javascript and the challenges associated with dates and the other is ServiceNow. Given that users can have their own date format makes it even more challenging. That said, we are given some tools (albeit undocumented ones) that can help improve the situation.

I ran across some information (thanks to John Roberts) that helps the situation drastically. The key comes down to a couple global variables defined by the system and a function provided that helps use those variables. 

The variables give us the date and datetime formats for the logged in user, and the function lets us use those to get something we can work with a little easier.

User datetime format:
g_user_date_time_format
 
User date format:
g_user_date_format
 
These used with the getDateFromFormat function gives us an easy way to get a value for the date (essentially a Unix Timestamp). That value can then be used directly or immediately passed into a Javascript Date object to allow for reformatting, testing, or whatever else is needed.
 
Here are a few functions that I put together to simplify the date validation process that is so often needed.
 
Test for valid DateTime based on user format:
function isValidDateTime(value){
    if(value == '' || value == undefined || value == null){
        return false;
    }
    return(getDateFromFormat(value, g_user_date_time_format) != 0);
}
 
Test for valid Date based on user format:
function isValidDate(value){
    if(value == '' || value == undefined || value == null){
        return false;
    }
    return (getDateFromFormat(value, g_user_date_format) != 0);
}

 

To take this a step further, you could easily add comparisons of dates or add custom date formats based on the values.

One thing to keep in mind with this, depending on where the date is coming from you may still have two date formats. Any of the dates that will work as shown are going to be the values in fields on a form or the Display Value of the date. If you're getting something from the server (e.g. via GlideAjax) as just a value then it will be stored in the regular system format of YYYY-MM-DD HH:MM:SS.

Hopefully this helps save you some of the frustration I've experienced over the years.


What's New

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

Archives
2021 (2)
  September (1)
  May (1)
2019 (1)
  August (1)
2018 (3)
  August (1)
  April (1)
  January (1)
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)