DateCompare function in Legal Script Include not functioning as expected

Amy V2
Mega Sage

I have a record producer that is using an out-of-the-box script include which has a Read-only protection policy. For my particular needs, I had to alter that script include so I made a copy. However, I have not altered the compareDates function code at all.

 

When I submit the record producer, there is on onSubmit client script which calls the script include, comparing two dates on the request (Start Date and End Date) to validate that the start date is before the end date. The function in the script include also calls the OOB DateUtils.compareDates function.

 

In testing, we entered a start date in 2023 and an end date in 2024 (an anticipated end date in the record producer), and the validation is saying the start date must be earlier than the end date.

 

I added a gs.info line to the script include and the dates are being converted from the record producer to something that looks completely odd to me. Can anyone provide any insight?

 

ETA Log message: AV - start date is 0018-08-16 end date is 0008-08-16

 

Here is the code:

 

Catalog Client Script Date Validation (onSubmit):

function onSubmit() {
if (g_scratchpad.areDatesValid) {
return true;
}

g_scratchpad.actionName = g_form.getActionName();
g_scratchpad.areDatesValid = false;
var variableDescriptor = g_scratchpad.coi.variableDescriptor;
var SUBCATEGORY_NON_WFRD = variableDescriptor.conflictTypes.nonWFRD.id;
var SUBCATEGORY_BOARD_SERVICE = variableDescriptor.conflictTypes.boardService.id;
var SUBCATEGORY_OUTSIDE_EMPLOYMENT = variableDescriptor.conflictTypes.outsideEmployment.id;
var SUBCATEGORY_GOVERNMENT_OFFICIAL = variableDescriptor.conflictTypes.governmentOfficial.id;
var SUBCATEGORY_FINANCIAL_INTEREST = variableDescriptor.conflictTypes.financialInterest.id;
var CONFLICT_TYPES = [SUBCATEGORY_OUTSIDE_EMPLOYMENT, SUBCATEGORY_BOARD_SERVICE, SUBCATEGORY_NON_WFRD, SUBCATEGORY_GOVERNMENT_OFFICIAL, SUBCATEGORY_FINANCIAL_INTEREST];

var conflictType = g_form.getValue('conflict_type');
var startDate = '';
if (conflictType == '2161f2481bc9bd10c7f16316624bcbd2') { //Financial Interest
startDate = g_form.getValue('date_interest_acquired_coi');
} else {
startDate = g_form.getValue('start_date');
}
var endDate = g_form.getValue('end_date');
if (CONFLICT_TYPES.indexOf(conflictType) > -1 && startDate && endDate) {
var coiAjax = new GlideAjax('sn_lg_coi.LegalCoiWfrdAjax');
coiAjax.addParam('sysparm_name', 'compareDates');
coiAjax.addParam('sysparm_start_date', startDate);
coiAjax.addParam('sysparm_end_date', endDate);
coiAjax.setScope('sn_lg_coi');
coiAjax.getXMLAnswer(function(answer) {
var result = JSON.parse(answer);
if (result === 1) {
var errorMessage = getMessage('Start date must be earlier than the End date.');
g_form.addErrorMessage(errorMessage);
return;
}
g_scratchpad.areDatesValid = true;
g_form.submit(g_scratchpad.actionName);
});
} else {
return true;
}
return false;
}

 

LegalCoiWfrdAjax.compareDates:

compareDates: function() {
var startDate = this.getParameter('sysparm_start_date');
var endDate = this.getParameter('sysparm_end_date');
var startGlideDate = new GlideDate();
startGlideDate.setValue(startDate);
var endGlideDate = new GlideDate();
endGlideDate.setValue(endDate);
gs.info('AV - start date is ' + startGlideDate + ' end date is ' + endGlideDate);
return JSON.stringify(DateUtils.compareDates(startGlideDate, endGlideDate));
},

 

DateUtils.compareDate:

DateUtils.compareDates = function(date1, date2) {

if (date1 > date2)
return 1;
else if (date1 < date2)
return -1;
return 0;
};

 

Note: Also cross-posted in Community > LSD Forum.

1 ACCEPTED SOLUTION

Amy V2
Mega Sage

I was able to find a solution by parsing out the MM, DD, and YYYY using slice and then joining them back together in the format YYYY-MM-DD before sending it to the script include. Thanks!

View solution in original post

4 REPLIES 4

Ethan Davies
Mega Sage
Mega Sage

Instead of using an onSubmit Client Script, GlideAjax, and a bunch of code in Script Includes, you can do this just In the example below, you can create a Catalog UI Policy on your record producer with the following conditions to ensure an End Date cannot be before a Start Date: 

EthanDavies_0-1702598380341.png

You can then execute some code in the UI Policy to handle your error message, and field message, and even clear out the Date Value that caused the issue. Add the code relevant to your requirements.

EthanDavies_3-1702598714940.png

Putting it all together, you get something like this.

P.S. These fields should be mandatory in your use case if we are clearing them when the error is shown.

EthanDavies_2-1702598673173.png

Let me know if this helps you or not, or if you really need to do this with a Client Script as opposed to a UI Policy.

 

Hi Ethan,

 

I actually do date comparisons on other record producers the same way. However, I was trying to keep this particular process as close to out-of-the-box as possible, so I was trying to fit it in using what ServiceNow already had in place.

 

Thanks!

Amy V2
Mega Sage

I was able to find a solution by parsing out the MM, DD, and YYYY using slice and then joining them back together in the format YYYY-MM-DD before sending it to the script include. Thanks!

Great to hear Amy - always a good feeling when you solve your own problem!