Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Milliseconds to GlideDateTime in scoped application

drberg
Giga Contributor

In a scoped application: how do I set a millisecond epoch value from an API to GlideDateTime?

I have tried this

var dt = new GlideDateTime();

dt.setNumericValue(1476024386630);

gs.info(dt.getValue());

But I only get:

java.lang.SecurityException: Function setNumericValue is not allowed in <scopename>. There is no alternative for this method
Caused by error in <refname> at line 12

setNumericValue() is legacy, but works fine in Global.

4 REPLIES 4

santoshsahoonis
Kilo Guru

Use add() and subtract()



var gdt = new GlideDateTime();


gdt.subtract(gdt.getNumericValue());// gets the date to 0


gdt.add(1476024386630);// The milliseconds you want to get in the date


gs.info(gdt.getNumericValue());


gs.info(gdt.getDisplayValue());


Thanks! Works like a charm!


A cleaner method is to use setValue(milliseconds) instead of subtract then add.

 

The add subtract also worked for me.

 

 

Edit: Initially it didn't appear to work for me because of an issue with one of my transform maps.

My milliseconds from epoch was 1565123722502 which is  Tuesday, August 6, 2019 1:35:22.502 PM  PST

But when i inspect the table i inserted into, i see 2019-08-07 03:35:22

I traced the root cause to writing the GlideDateTime.toString() into the import set as UTC then the transform map assumes the string is in local time, when it's really in UTC. I resolved this by inserting into the import set using getDisplayValue().

 

 

Nevermind, I get the same result with .setValue() . It might have something to do with the interaction with import sets.