Date/Time script on Record Producer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2018 12:55 PM
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");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2018 07:15 AM
Is your variable a date or a date / time field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2018 07:19 AM
It's a date/time field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2018 07:31 AM
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');
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2018 01:04 PM
If you are doing this via a client scrip you will need to call a script include. I would recommend following this blog.