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.

GlideDate is not defined, but is in the documentation

John Prouty
Kilo Guru

I have a Client Script that needs to validate a date field (not a date/time field).  I am trying to use GlideDate, but I get an error that it is not defined.

find_real_file.png

What I am really trying to do is to compare a date that was entered into a field (sched_implement_date) and compare it to Today + total_est_duration.  GlideDate doesn't have an Add method and my dates don't have Time (so it is really difficult to use GlideDateTime).  If GlideDate would work, I think the following code would meet my needs:

field = g_form.getValue("sched_implement_date");

// Sched Implementation Date must be >= Today + Total Est Duration
var duration = g_form.getValue("total_est_duration");
g_form.addErrorMessage("Duration: " + duration);
if (duration != 0) {
   var gdt = new GlideDate();
   var today = new GlideDate(gdt.getDisplayValue());

   var impldate = new GlideDate();
   impldate.SetDisplayValue(field);

   calcduration = GlideDate.subtract(today, impldate);
   g_form.addErrorMessage("Today: " + today + "Impl Date: " + field + "Calculated Duration: " + calcduration);
   if (calcduration < duration) {
      g_form.showFieldMsg("sched_implement_date", "Sched Implementation Date is before Today + Total Dev. Est. Duration. Sched Implementation Date must be changed.", "error");
      Passed = 'NO';
      }
   }

Any suggestions on how to get GlideDate to work?  OR another way to do the samething without GlideDate?

1 ACCEPTED SOLUTION

John Prouty
Kilo Guru

I googled javascript for these functions.

Here is the final code:

// Sched Implementation Date is Required
field = g_form.getValue("sched_implement_date");
if (field === '') {
   g_form.showFieldMsg("sched_implement_date", "Sched Implementation Date is missing. It is a required field.", "error");
   Passed = 'NO';
}
else {
   var duration = g_form.getValue("total_est_duration");
   if (duration != 0) {
      var durnum = parseInt(duration);
      var ImplDate = new Date(field);
      var minImplDate = new Date();
      minImplDate.setDate(minImplDate.getDate() + durnum);

      if (ImplDate < minImplDate) {
         g_form.showFieldMsg("sched_implement_date", "Sched Implementation Date is not after: Today + Total Dev. Est. Duration (" + minImplDate.toDateString() + "). Sched Implementation Date must be changed.", "error");
         Passed = 'NO';
      }
   }
}

View solution in original post

3 REPLIES 3

Elijah Aromola
Mega Sage

Glidedate is a server method, you cannot use it client side. If you must interact with it you can call a script include via GlideAjax.

Allen Andreas
Tera Patron

Hi,

Not only what Elijah mentioned, but this section:

   var impldate = new GlideDate();
   impldate.SetDisplayValue(field);

is not correctly. setDisplayValue, not SetDisplayValue.

Please refer to documentation: https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideDateScope...

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


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

John Prouty
Kilo Guru

I googled javascript for these functions.

Here is the final code:

// Sched Implementation Date is Required
field = g_form.getValue("sched_implement_date");
if (field === '') {
   g_form.showFieldMsg("sched_implement_date", "Sched Implementation Date is missing. It is a required field.", "error");
   Passed = 'NO';
}
else {
   var duration = g_form.getValue("total_est_duration");
   if (duration != 0) {
      var durnum = parseInt(duration);
      var ImplDate = new Date(field);
      var minImplDate = new Date();
      minImplDate.setDate(minImplDate.getDate() + durnum);

      if (ImplDate < minImplDate) {
         g_form.showFieldMsg("sched_implement_date", "Sched Implementation Date is not after: Today + Total Dev. Est. Duration (" + minImplDate.toDateString() + "). Sched Implementation Date must be changed.", "error");
         Passed = 'NO';
      }
   }
}