- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2025 04:03 AM
Hi Team,
As per my requirement,
when the priority is changing on the change form,
1. condition = when priority is 1
planned start date and planned end date only allows past date
2. condition= when priority is 2, 3 or 4
planned start date and planned end date only allows future date
how to achieve this?
Regards,
Abhilasha G T
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2025 06:12 AM
you will require onChange client script on Priority
Something like this, please enhance it further as per your requirement
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('planned_start_date');
g_form.hideFieldMsg('planned_end_date');
// Get the planned start and end date fields
var plannedStartDate = g_form.getValue('planned_start_date');
var plannedEndDate = g_form.getValue('planned_end_date');
// Convert dates to JavaScript Date objects
var startDate = new Date(plannedStartDate);
var endDate = new Date(plannedEndDate);
var currentDate = new Date();
// Condition for Priority 1
if (newValue == '1') {
if (startDate > currentDate || endDate > currentDate) {
g_form.showFieldMsg('planned_start_date', 'Planned start date must be in the past for Priority 1', 'error');
g_form.showFieldMsg('planned_end_date', 'Planned end date must be in the past for Priority 1', 'error');
g_form.clearValue('planned_start_date', '');
g_form.clearValue('planned_end_date', '');
}
}
// Condition for Priority 2, 3, or 4
if (newValue == '2' || newValue == '3' || newValue == '4') {
if (startDate < currentDate || endDate < currentDate) {
g_form.showFieldMsg('planned_start_date', 'Planned start date must be in the future for Priority 2, 3, or 4', 'error');
g_form.showFieldMsg('planned_end_date', 'Planned end date must be in the future for Priority 2, 3, or 4', 'error');
g_form.clearValue('planned_start_date', '');
g_form.clearValue('planned_end_date', '');
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2025 09:11 AM
Hi @Abhilasha G T ,
I tried your problem in my PDI please check below code
Create onChange Client script in Priority field and add below code
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get the planned start and end date fields
var plannedStartDate = g_form.getValue('planned_start_date');
var plannedEndDate = g_form.getValue('planned_end_date');
var startDate = new Date(plannedStartDate);
var endDate = new Date(plannedEndDate);
var currentDate = new Date();
if (newValue == '1') {
if (startDate > currentDate || endDate > currentDate) {
alert("Please select any date in past");
g_form.clearValue('planned_start_date', '');
g_form.clearValue('planned_end_date', '');
}
}
// Condition for Priority 2, 3, or 4
if (newValue == '2' || newValue == '3' || newValue == '4') {
if (startDate < currentDate || endDate < currentDate) {
alert("Please select any date in future");
g_form.clearValue('planned_start_date', '');
g_form.clearValue('planned_end_date', '');
}
}
}
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak