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

Is your variable a date or a date / time field?

It's a date/time field

Lets try adding a couple of alerts into you code and see if we get the expected results.

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

//Type appropriate comment here, and begin script below
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDate');
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
var date = newValue.split(' ');
alert('Date: ' + date);
alert ('Date Index: ' + date[0]);
alert ('Answer: ' + answer);
if (date[0] > answer){
alert('Start date cannot be in the future');
g_form.clearValue('u_outage_start');
}
}
}

Brian Lancaster
Tera Sage

If you are doing this via a client scrip you will need to call a script include.  I would recommend following this blog.