Due date on problem task form should be restricted to 2 months and user should not be able to select past date
Assistant
To implement the requirement in ServiceNow where the due date on the Problem Task form is restricted to no more than 2 months from the current date and does not allow past dates, you can utilize a client script.
Follow these steps to create a client script:
Navigate to the Client Scripts:
Go to your ServiceNow instance.
In the Application Navigator, type Client Scripts and open it.
Create a New Client Script:
Click on the "New" button to create a new client script.
Fill in the Client Script Details:
Name: Restrict Due Date in Problem Task
Table: Problem Task [problem_task] (or the appropriate table if your implementation is different)
Type: onChange (For changing the date field dynamically, we will use onLoad script)
Add the Script:
Here’s a sample script that you can use:
javascript
function onLoad() {
// Get the current date
var today = new Date();
// Calculate the maximum date (2 months from today)
var maxDate = new Date();
maxDate.setMonth(today.getMonth() + 2);
// Get the due date field
var dueDateField = g_form.getField('due_date'); // Replace 'due_date' with your actual field name
// Set the minimum date (today)
g_form.setMinValue('due_date', today.toISOString().split('T')[0]);
// Set the maximum date (2 months from today)
g_form.setMaxValue('due_date', maxDate.toISOString().split('T')[0]);
// Optionally, reset the date if it is outside the range
var dueDate = g_form.getValue('due_date');
if (dueDate < today.toISOString().split('T')[0] || dueDate > maxDate.toISOString().split('T')[0]) {
g_form.setValue('due_date', '');
g_form.showFieldMsg('due_date', 'Due date must be in the range of today to 2 months ahead.', 'error');
}
}
Orientation:
Ensure that you replace 'due_date' with the actual field name used in your Problem Task table for Due Date if it's named differently.
This script sets the minimum and maximum values for the due date and also ensures that if an invalid date is out of the specified range is entered, it’s reset, and a message is shown.
Active:
Make sure the Active checkbox is checked.
Save the Client Script:
After adding the code, save your script.
Testing:
Test the form by opening a Problem Task and trying to set different dates. Ensure the functionality behaves as expected, allowing only future dates up to 2 months from the current date.
By following this procedure and leveraging the provided code, you should be able to effectively restrict the due date input on the Problem Task form in ServiceNow according to your requirements.