Date/Time script on Record Producer

booher04
Tera Guru

I put a script together in the script section of a record producer.  I have it working partially but can't seem to figure out what I'm missing.  I want my field(u_outage_start) to not be allowed to be in the future.  If it is, I want it to pop a message that says it can't be, and let the user correct the date on the portal page(record producer).  Right now, it works, gives me the message but it still submits the catalog item and creates the incident with the incorrect outage date/time. 

Here's the script:

var initialDate = new GlideDateTime(producer.u_outage_start).getDate(); 
var today = new GlideDateTime().getDate();
var diff = gs.dateDiff(today,initialDate, true);

if(diff > 0){

gs.addInfoMessage("Outage Start Date cannot be after today's date");

}

18 REPLIES 18

Jace Benson
Mega Sage

So, you'd need to check this in a client script;
Something like this will get you close

//Validate Date is after set time
function onSubmit() {
  var returnVal = false;
  // Type appropriate comment here, and begin script below
  // validate that the given field's date is at least or equal to the ms to add.
  var field = 'project_deadline';
  // this is a week
  var msToAdd = 1000 * 60 * 60 * 24 * 7;//ms * sec * minutes * hours * days
  var errorMsg = 'This must be at least a week out.';
  /****************************************************/
  /*  You shouldn't have to modify anything below     */
  /****************************************************/
  var now = new Date();
  var givenDate = new Date(g_form.getValue(field));
  // forwhatever reason, at this point this returns 9/24 when you select 9/25
  // givenauthor: 'jace'
  // date:Mon Sep 24 2017 19:00:00 GMT-0500 (Central Daylight Time)[1506347252815]
  // when you select 9/25
  givenDate.setDate(projectDeadline.getDate() + 1);//so add a day
  givenDate.setHours(now.getHours());
  givenDate.setMinutes(now.getMinutes());
  givenDate.setSeconds(now.getSeconds());
  givenDate.setMilliseconds(now.getMilliseconds());
  // now returns;
  // givenauthor: 'jace'
  // date:Mon Sep 25 2017 08:47:32 GMT-0500 (Central Daylight Time)[1506347252815]
  var nextWeek = new Date();
  nextWeek.setTime(nextWeek.getTime() + msToAdd);
  // console.log('projectDeadline: ' + projectDeadline + '[' + projectDeadline.getTime() + ']');
  // console.log('weekAhead     : ' + weekAhead      + '[' + weekAhead.getTime()      + ']');
  var givenDateGreaterOrEqualToNextWeek = givenDate.getTime() >= nextWeek.getTime();
  //  console.log('givenDateGreaterOrEqualToNextWeek: ' + givenDateGreaterOrEqualToNextWeek);
  if (givenDateGreaterOrEqualToNextWeek) {
    returnVal = true;
  } else {
    g_form.hideFieldMsg(field, true);
    g_form.showFieldMsg(field, errorMsg, 'error');
  }
  return returnVal;
}

This one seems to be the closest, however I need it to be reversed:

//Validate Date is after today
//GwtDate not available on service portal
function onSubmit() {
//Type appropriate comment here, and begin script below
//validate that the start date is before the today's date
var field = 'start_date';
var st = g_form.getValue(field);
var newTime = new GwtDate(st);
var tm = new GwtDate();
tm.now();
tm.subtractHours(24);
if (newTime.compare(tm, true) < 0) {
g_form.hideFieldMsg(field, true);
g_form.showFieldMsg(field, 'Start date must be after the today\'s date.', 'error');
return false;
}
}

 

I do not have a field called "start date" the field I have is u_outage_start.  Also, I need this to run on the Service Portal and this will not.  

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi,

 

You should create an OnChange client script to validate this. You may find the below thread helpful.

https://community.servicenow.com/community?id=community_blog&sys_id=467c62e1dbd0dbc01dcaf3231f9619ad

 

-Pradeep Sharma

Here's what I came up with.  I created a script include and created this onChange script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var cdt = g_form.getValue('u_outage_start'); //first Date/Time field

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeBeforeNowBool');
ajax.addParam('sysparm_fdt', cdt);
ajax.getXML(doSomething);

function doSomething(response){
if (cdt > ajax)
//var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Outage start date cannot be after today's date.");
}

}

 

The issue I have is that it's not checking to make sure var cdt is greater than var ajax.  It's just always popping the alert.  I need the alert to only generate when the date/time is after todays date.