Compare a field date with current time using Client Script.

prabhmeet
Giga Expert

Hi,

I want that on change_request table, planned start date should always be 24hrs ahead of current time.

I have done this successfully using Business Rule, But I have to do this using Client Script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below


var start = g_form.getValue('start_date');
var dttype = 'hour';
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', start);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");

if (answer >= 1)

{g_form.addErrorMessage('Selected time should be more than 24 hours of current time');

}
}

it is not working. Can someone help with the code?

 

1 ACCEPTED SOLUTION

I see, if you only want to write in client script. You can using pure javascript in stead of ajax call.

Below is the sample: You write this in your client script then you should able to get day ago date and from there u can continue your checking logic.

var d = new Date();
d.setDate(d.getDate() - 1);
alert(d.toString());

View solution in original post

16 REPLIES 16

I know you are writing client script.

And you are writing client script to call "script include" class named as "ClientDateTimeUtils"

So the 1st thing you need to check is to make sure your script include can call by client script via ajax call.

find_real_file.png

I am new to ServiceNow, I have no idea if I need to write a Script Include also with this. 

Can't this be done only with writing a Client Script without Script Include?

If not, what code should be written in the contents of the script Include?

var ClientDateTimeUtilsAjax= Class.create();
ClientDateTimeUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 --- contents ---

type: 'ClientDateTimeUtilsAjax'
});

I see. If u just want to do it via client script, then don't need to use ajax call.

You should explore pure javascript function.

You can try below sample in your client script. You should able to get the 1 day ago date. Then from there you can do your checking.

var d = new Date();
d.setDate(d.getDate() - 1);
alert(d.toString());
 

I see, if you only want to write in client script. You can using pure javascript in stead of ajax call.

Below is the sample: You write this in your client script then you should able to get day ago date and from there u can continue your checking logic.

var d = new Date();
d.setDate(d.getDate() - 1);
alert(d.toString());

thanks. I have written the below mentioned code. The error message comes up,if I put in wrong date, but goes away does not stick on the screen. Why is this happening?

 

function onSubmit(){
//Type appropriate comment here, and begin script below


var start = g_form.getValue('start_date');
var today = new Date();
today.setDate(today.getDate());
if (start.toString() <= today.toString()){
g_form.addErrorMessage("Planned Start Date cannot be less than 24 hrs ahead in future.");
}
}