Compare two dates in Catalog Client Script

JJG
Kilo Guru

Hello,

I have 3 date fields on my record producer.

1) Start date - 'pay_period_start'

2) End date - 'pay_period_end'

2) Date - 'date'

I need to compare the 'date' with the Start Date & End date. This will decide what happens on the form. Here is my on change client script so far. it is not working:

 

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

var dateValue = g_form.getValue('date');
var start = g_form.getValue('pay_period_start');
var end = g_form.getValue('pay_period_end');

if (dateValue < start || dateValue > end) {
g_form.showFieldMsg('date', ':Please provide an explanation.', 'error');
g_form.setVisible('past_date_hours_explanation', true);
g_form.setMandatory('past_date_hours_explanation', true);
}

else {
g_form.hideFieldMsg('date');
g_form.setMandatory('past_date_hours_explanation', false);
g_form.setVisible('past_date_hours_explanation', false);
}
}

1 ACCEPTED SOLUTION

use glide ajax here, and in client callable script include compare it, based on that return true or false , you will get more flexibility in date comparison  if you will use script include + glideajax.

 

sample code mentioned in below thread. 

https://community.servicenow.com/community?id=community_question&sys_id=56c70fe9db1cdbc01dcaf3231f96...

View solution in original post

7 REPLIES 7

Harsh Vardhan
Giga Patron

what's not working here ?

is it going inside the if block ? can you add some alert inside the if block 

 

updated script:

 

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

var dateValue = g_form.getValue('date');
var start = g_form.getValue('pay_period_start');
var end = g_form.getValue('pay_period_end');

if (dateValue < start || dateValue > end) {

alert('coming inside if ');
g_form.showFieldMsg('date', ':Please provide an explanation.', 'error');
g_form.setVisible('past_date_hours_explanation', true);
g_form.setMandatory('past_date_hours_explanation', true);
}

else {

alert('coming inside else ');

g_form.setMandatory('past_date_hours_explanation', false);
g_form.setVisible('past_date_hours_explanation', false);
}
}

it is not comparing the date fields correctly. No matter what 'date' is selected the 'if' code is still executing. Very confused.

can you break your condition for just troubleshooting your script , 

 

eg: first check dateValue < start  and see are you getting if block alert or not , if you will get 

then check for dateValue > end using inside the if block ,

 

let me know the first condition is working here or not, date field value should be less than pay_period_start field value , 

 

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

var dateValue = g_form.getValue('date');
var start = g_form.getValue('pay_period_start');
var end = g_form.getValue('pay_period_end');

if (dateValue < start ) {

alert('coming inside if ');
g_form.showFieldMsg('date', ':Please provide an explanation.', 'error');
g_form.setVisible('past_date_hours_explanation', true);
g_form.setMandatory('past_date_hours_explanation', true);
}

else {

alert('coming inside else ');

g_form.setMandatory('past_date_hours_explanation', false);
g_form.setVisible('past_date_hours_explanation', false);
}
}

when I just use: if (dateValue < start) No matter what date I select the IF always executes

When I just user: if (dateValue > end) No matter what date I select the ELSE always executes

 

The <   &   > signs are not comparing the date variables properly