Restrict past date selection in ServiceNow
How to restrict past date selection in ServiceNow
Restrict Past Date at the Form level
Case 1
Simple use case:
Let's say you want to prevent users from selecting a past date on the Start Date field.
Solution 1
Client Script: onChange
Field name: u_start_date
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Start Date should not allow past date
// Get the current date
var currentDate = new Date();
// Get the Start Date selected by the User
var startDate = new Date(g_form.getValue('u_start_date'));
// Check if the Start Date is in the past
if (startDate.valueOf() < currentDate.valueOf()) {
g_form.addErrorMessage('Select a future date for the Valid From Date');
g_form.clearValue('u_start_date'); // Clear the value if the user selected a past date
}
}
This is the error that should be populated when selecting a past date:
Reference:
Solution 2
UI Policy
You can also implement this via the UI Policy.
Condition: Start Date before Today
Script:
function onCondition() {
// Populate error
g_form.addErrorMessage('Select a future date for the Valid From Date');
g_form.clearValue('u_start_date'); // Clear the value if the user selected a past date
}
Reference:
Case 2
A bit harder use case:
Let's say you want to ensure that the "Valid To" date is not before the "Valid From" date
Solution
Client Script: onChange
Field name: Valid To
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get the values of the "Valid From" and "Valid To" fields
var validFromDate = g_form.getValue('valid_from');
var validToDate = g_form.getValue('valid_to');
// Check if both fields have values
if (validFromDate && validToDate) {
// Convert the values to Date objects
var fromDate = new Date(validFromDate);
var toDate = new Date(validToDate);
// Compare the dates
if (fromDate > toDate) {
// Reset the "Valid To" field
g_form.setValue('valid_to', '');
// Display an error message
g_form.addErrorMessage('Valid To date cannot be before Valid From date.');
}
}
}
This is the error that should be populated when selecting the "Valid To" date before the "Valid From" date:
Case 3
Harder use case:
Let's say you want to prevent submitting a record if the "Valid From" date has already been submitted in the past between the "Valid From" and "Valid To" dates for the same company.
For this, you will need to perform a more detailed query to check for overlapping date ranges for the same company.
Solution
Before Business Rule
insert
Script:
(function executeRule(current, previous /*null when async*/) {
// Get the values of the "Valid From," "Valid To," and company fields
var validFromDate = current.valid_from;
var validToDate = current.valid_to;
var companyId = current.company;
// Check if all fields have values
if (validFromDate && validToDate && companyId) {
// Query the table to check for existing records with overlapping date ranges
var gr = new GlideRecord('x_393895_credit_0_company_extensible'); // Replace 'your_table_name' with the actual table name
gr.addQuery('company', companyId);
gr.addQuery('sys_id', '!=', current.sys_id); // Exclude the current record when updating
gr.addQuery('valid_from', '<=', validToDate);
gr.addQuery('valid_to', '>=', validFromDate);
gr.query();
if (gr.hasNext()) {
// Record with overlapping date range already exists
// Display an error message
current.setAbortAction(true);
gs.addErrorMessage('A Company Rate record with overlapping date range already exists for the same company.');
}
}
})(current, previous);
Restrict Past Date at the Catalog Item level
References:
- https://www.servicenow.com/community/developer-forum/how-to-restrict-past-date-selection/m-p/1437062
- https://www.servicenow.com/community/itsm-articles/how-to-restrict-past-date-in-date-field-using-onchange-client/ta-p/2308887
Comments
Post a Comment