How to use field type Counter / GlideCounter

Sebastian R_
Kilo Sage

Today I ran across the field "Recorded at" (sys_recorded_at) on the sys_update_xml table. This field is displayed as a normal date/time field but is of type "Counter".

From the documentation (https://hi.service-now.com/kb_view.do?sysparm_article=KB0745434) I know that "Recorded at" has a sub-millisecond resolution and is therefore more precise as a normal date/time field.

The value which is stored is e.g. "169e8a589000000001".

In the dictionary I found the JavaClass GlideCounter, but no documentation for it anywhere.

find_real_file.png

Has anyone an idea how  to work with such a field and how to use the GlideCounter correctly (also GlideCounter has some other methods)?

4 REPLIES 4

Uncle Rob
Kilo Patron

I used Xplore to dig into this, but couldn't find much.  Here's the methods we can see:

 

I tried using a bunch of them, but I got loads of "illegal access" errors, which maybe means they're only usable in certain circumstances.  I tried the Default Value javascript listed in your screenshot, but the result was indecipherable.  It changes any time you run the command too.  Additionally, seems you can run any table name as a parameter.

 

Might be one of those functions that just isn't meant to be used by the public.

If you want to experiment with Xplore and see if you can find out anything more....
Download Explore (it's free)
Learn to use Explore

Robert, thanks for the leg-work on this.

An example:

  • If you alter the ACL's on the sys_update_version table, you can write a background script:
    var gr = new GlideRecord('sys_update_version');
    gr.addQuery('sys_id', '476ac3e3db79f300bd4cff9bfe9219d7');
    gr.query();
    if (gr.next()) {
    	gr.sys_recorded_at = "16a7d0780c20000001";
    	gr.update();
    }​
    and it will update the "Recorded at" field.
    find_real_file.png

What I'm curious about is how ServiceNow converts "16a7d0780c20000001" into a readable Date/Time.

I will figure this out.  Looks like I'll have to set breakpoints in my browser's developer console and painstakingly F10 all the way until I see what hash algorithm/function they're using.

Here's how to convert a Counter field into a GlideDateTime:

function counterToGDT(s) {
    var ms = parseInt(s.slice(0, -7), 16);
    var gdt = new GlideDateTime();
    gdt.setValue(ms);
    return gdt;
}

var gdt = counterToGDT(gr.sys_recorded_at);
gs.info('{0} == {1}', gr.sys_recorded_at.getDisplayValue(), gdt.getDisplayValue());

// 2021-03-11 13:32:21 == 2021-03-11 13:32:21

The above works in scoped or global. Here's another method, but since GlideCounter is not available in scoped, this works in global only:

var d = GlideCounter.getDate('17822c690e90000001');
var gdt = new GlideDateTime();
gdt.setValue(d.time);
gdt.getDisplayValue();

// 2021-03-11 13:32:21