Show a field if Date/Time field is 'more than 12 hours' from nowDateTime?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2017 07:34 PM
Hi - I am not that great at date/time scripts.
I am looking for a client script to compare a custom date/time field and if the date/time is more than 8 hours ago, I want to display a field.
Can someone help me out/provide guidance?
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2017 12:19 PM
Hi Shishir
I did update it. No dice; fields do not display...they remain hidden even when the field is changed.
I left both client scripts in place (onChange and onLoad).
var MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
nowDateTime: function () {
var seconds = '28800'; //no Seconds in 8 hours
var opdt = this.getParameter('sysparm_dt');
var diff = gs.dateDiff(gs.nowDateTime(), opdt, true);
if(parseInt(diff) > parseInt(seconds))
return true;
else
return false;
}
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2017 12:28 PM
On question: I did enter a date within 12 hours and the fields disappeared, but when I changed the date to outside 12 hours, the fields did not appear.
I think that's the expected behavior of the code, if issue start time is less the 12 hours then return from script include would be false and then the fields (u_why_is_issue_urgent and u_more_escalation_info) will be set to visible false (means disappear) and when start time is greater the 12 hours then return from script include would be true and then the fields (u_why_is_issue_urgent and u_more_escalation_info) will be set to visible true (means do not disappear)
if you want the field to disappear when it exceeds 8 hours and should not be hidden when it's with-in 8 hours then change the if condition in script include with below code and try.
if(parseInt(diff) < parseInt(seconds)) // diff should be less than 8 hours so that it will returns true to make the field visible else return false to hide the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2017 12:35 PM
Hm I commented out my setting fields visible etc, and left in your
'Alert' lines of code.
It doesn't matter if the date is within 8 hours or exceeds 8 hours, I get
the 'Good to Go' alert prompt, regardless.
On Sat, Jul 15, 2017 at 3:29 PM, explorenow <
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2017 12:41 PM
If I add 'else if' answer is false, then 'good to go' then no prompts are
displayed.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var opdt = g_form.getValue('u_issue_start_time'); //Replace the date field
name accordingly
var ajax = new GlideAjax('MyDateTimeAjax');
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.addParam('sysparm_dt', opdt);
ajax.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var answer =
serverResponse.responseXML.documentElement.getAttribute("answer");
if(answer == 'true'){
//g_form.setVisible('u_why_is_issue_urgent', true);
//g_form.setVisible('u_more_escalation_info', true);
alert('Selected time is more than 12 hours');
}
else if(answer == 'false') {
//g_form.setVisible('u_why_is_issue_urgent', false);
//g_form.setVisible('u_more_escalation_info', false);
alert('Good to GO');
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2017 12:53 PM