The CreatorCon Call for Content is officially open! Get started here.

Unable to use .getByFormat("MM-dd-yyyy"); in script include

Kumar38
Kilo Sage

I am passing 3 date fields to script include from a client script . Though the dates are passed in the same format ("MM-dd-yyyy") as I require.

I'd like to ensure , it doesn't affect users from various places . hence using .getByFormat("MM-dd-yyyy") . But it doesnt work as I printed logs and it prints undefined

var ahm_eos = this.getParameter('sysparm_ahm_eos');
        var ahm_eol = this.getParameter('sysparm_ahm_eol');
        var ahm_tspd = this.getParameter('sysparm_ahm_tspd')


ahm_eos = ahm_eos.getByFormat("MM-dd-yyyy");
        ahm_eol = ahm_eol.getByFormat("MM-dd-yyyy");
        ahm_tspd = ahm_tspd.getByFormat("MM-dd-yyyy");

// THE ABOVE VALUES ARE PRINTED AS UNDEFINED IN LOGS
7 REPLIES 7

Allen Andreas
Administrator
Administrator

Hello,

You'd want to take the values you're getting from the client script and then build out appropriate GlideDate/GlideDateTime objects with them, then use the getByFormat per your needs.

Here's an exact example: https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_GlideDateScopedAP...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Tony Chatfield1
Kilo Patron

Hi, the details of your issue are not clear as your code is incomplete, you don't indicate what data types the getParameter() values are.

But I suspect that  issue relates to the way you assign the formatted result back to the date object it was sources from, try this in a background window.

var ahm_eos = new GlideDate();

JSUtil.logObject(ahm_eos);

ahm_eos = ahm_eos.getByFormat("MM-dd-yyyy");

JSUtil.logObject(ahm_eos);

If you can update this thread with clear and specific details of your configuration and intentions, the forum may be able to assist otherwise we can only guess at your objectives.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Kumar,

The problem probably is that values passed from the client is in String and .getByFormat() requires a GlideDate object.

var ahm_eos = this.getParameter('sysparm_ahm_eos');
var ahm_eol = this.getParameter('sysparm_ahm_eol');
var ahm_tspd = this.getParameter('sysparm_ahm_tspd');

var gdAhmEos = new GlideDate();
var gdAhmEol = new GlideDate();
var gdAhmTspd = new GlideDate();
gdAhmEos.setValue(ahm_eos);
gdAhmEol.setValue(ahm_eol);
gdAhmTspd.setValue(ahm_tspd);

ahm_eos = gdAhmEos.getByFormat("MM-dd-yyyy");
ahm_eol = gdAhmEol.getByFormat("MM-dd-yyyy");
ahm_tspd = gdAhmTspd.getByFormat("MM-dd-yyyy");

The code in the question also was missing a semicolon at the end of the following line.

var ahm_tspd = this.getParameter('sysparm_ahm_tspd')

Hi,

I tried the Code , this is how the log looks.

hm_eos----03-29-0020--ahm_eol----07-07-0020--ahm_tspd---01-23-0021