Catalog Client Script error message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2024 04:28 AM
Hello everyone,
I have created this Catalog Client Script OnChange so that past dates cannot be selected in the start_date variable. The problem is that the message is not displayed when selecting a past date, I don't know if it is because when the form is displayed in the employee center there is another method that I am not applying; any ideas.
Regards and thanks in advance
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Get the current date and time
var currentDate = new Date();
// Convert the selected date to a Date object
var selectedDate = new Date(newValue);
// Compare dates
if (selectedDate < currentDate) {
// Show warning message
g_form.showFieldMsg('start_date', 'Date should not be in the past', 'error');
// Clear the date field
g_form.setValue('start_date', '');
return;
}
// Hide any error messages if the date is valid
g_form.clearMessages();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 09:04 AM
Catalog client script are not very good at handling dates. You have two options.
1. Use a UI Policy with no code: https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...
2. user a catalog client script that calls a script include: https://www.servicenow.com/community/in-other-news/auto-populating-and-validating-date-fields/ba-p/2...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 09:19 AM
Hi @tomaslindev
Create a Script include and call it on client script.
sample :-
SI :-
var DateUtils = Class.create();
DateUtils.prototype = {
initialize: function() {},
isDateInPast: function(dateStr) {
var currentDate = new GlideDateTime();
var selectedDate = new GlideDateTime(dateStr);
if (selectedDate.getDisplayValue() < currentDate.getDisplayValue()) {
return true;
}
return false;
},
type: 'DateUtils'
};
CS:-
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('DateUtils');
ga.addParam('sysparm_name', 'isDateInPast');
ga.addParam('sysparm_dateStr', newValue);
ga.getXMLAnswer(function(response) {
var isPast = response.responseXML.documentElement.getAttribute('answer');
if (isPast === 'true') {
g_form.showFieldMsg('start_date', 'Date should not be in the past', 'error');
g_form.setValue('start_date', '');
} else {
g_form.clearMessages();
}
});
}