Comparing Relative Dates in Client Script Not Working in SOW View
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2024 03:00 PM - edited ‎10-30-2024 03:01 PM
Good Afternoon Everyone!
I'm having another issue with some date comparisons in a client script with Service Operations Workspace. The below script works in the legacy view and partially in SOW. Previously, I was having issues with the script as a whole which I resolved in another post. Now, I'm just having issues with the 5th, 6th, and 7th condition.
I'm trying to add conditions where:
Condition 5
Change request model is "normal"
priority is "3" or "4"
planned start date or end date is cleared if the planned start date is or is less than 13 days from now
ex: today is October 30th and the next ALLOWED date would be November 13th...if it's Nov. 12th dates are cleared
Condition 6
Change request model is "normal"
priority is "1" or "2"
planned start date or end date is cleared if the planned start date is or is before 1 days from now or after 13 days
ex: today is October 30th so dates would have to be a date on or between November 1st and November 12th
Condition 7
Change request model is "emergency"
planned start date or end date is cleared if the planned start date is or is before 4 hours from now or after 2 days
ex: today is October 30th 5pm so dates would have to be a date on or between October 30th 9:01pm and November 1st
Everything else in the script is working fine in SOW, but I'm not sure if I'm creating the date variables correctly for these 3 conditions to function in Service Operations Workspace? Any assistance is appreciated!
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var state = g_form.getValue('state');
var type = g_form.getValue('type');
var priority = g_form.getValue('priority');
var critsystem = g_form.getValue('u_critical_systems');
var cabreject = g_form.getValue('u_cab_rejected');
// Blackout periods for the change freeze conditions
var blackoutStart1 = new Date('2024-11-28 00:00:00');
var blackoutEnd1 = new Date('2024-11-29 23:59:59');
var blackoutStart2 = new Date('2024-11-25 00:00:00');
var blackoutEnd2 = new Date('2024-11-27 23:59:59');
var blackoutStart3 = new Date('2024-12-16 00:00:00');
var blackoutEnd3 = new Date('2025-01-02 23:59:59');
var blackoutStart4 = new Date('2024-12-01 00:00:00');
var blackoutEnd4 = new Date('2024-12-15 23:59:59');
//SLA conditions for normal change periods
var blackoutStart5 = new Date();
blackoutStart5.setDate(blackoutStart5.getDate() + 13);
var blackoutStart6 = new Date();
blackoutStart6.setDate(blackoutStart6.getDate() + 1);
var blackoutEnd6 = new Date();
blackoutEnd6.setDate(blackoutEnd6.getDate() + 13);
var blackoutStart7 = new Date();
blackoutStart7.setHours(blackoutStart7.getHours() + 4);
var blackoutEnd7 = new Date();
blackoutEnd7.setDate(blackoutEnd7.getDate() + 2);
var newValueDate = new Date(newValue);
// Nov 28-29th blackout condition
if ((state == '-5' || state == '-4') && newValueDate >= blackoutStart1 && newValueDate <= blackoutEnd1) {
g_form.clearValue('start_date');
g_form.clearValue('end_date');
g_form.showFieldMsg('start_date', 'November 28th - November 29th is a change freeze period. No changes may be scheduled during this time.', 'error');
}
// Nov 25-27th low priority blackout condition
else if ((state == '-5' || state == '-4') && type == 'normal' && (priority == '4' || priority == '3') && newValueDate >= blackoutStart2 && newValueDate <= blackoutEnd2) {
g_form.clearValue('start_date');
g_form.clearValue('end_date');
g_form.showFieldMsg('start_date', 'November 25th - November 27th is a change freeze period. Only High Priority or Emergency changes may be scheduled during this time.', 'error');
}
// Dec 16-Jan 2nd low priority blackout condition
else if ((state == '-5' || state == '-4') && type == 'normal' && (priority == '4' || priority == '3') && newValueDate >= blackoutStart3 && newValueDate <= blackoutEnd3) {
g_form.clearValue('start_date');
g_form.clearValue('end_date');
g_form.showFieldMsg('start_date', 'December 16th - January 2nd is a change freeze period. Only High Priority or Emergency changes may be scheduled during this time.', 'error');
}
// Dec 1-15th if change has critical systems blackout condition
else if ((state == '-5' || state == '-4') && critsystem && newValueDate >= blackoutStart4 && newValueDate <= blackoutEnd4) {
g_form.clearValue('start_date');
g_form.clearValue('end_date');
g_form.showFieldMsg('start_date', 'December 1st - December 15th is a change freeze period. No critical system changes may be scheduled during this time.', 'error');
}
// 5th condition low priority SLA
else if ((state == '-5' || state == '-4') && type == 'normal' && (priority == '4' || priority == '3') && cabreject === 'false' && newValueDate <= blackoutStart5) {
g_form.clearValue('start_date');
g_form.clearValue('end_date');
g_form.showFieldMsg('start_date', 'Insufficient Lead time for your Low/Moderate Change (14 days minimum)', 'error');
}
// 6th condition high priority SLA
else if ((state == '-5' || state == '-4') && type == 'normal' && (priority == '1' || priority == '2') && cabreject === 'false' && (newValueDate <= blackoutStart6 || newValueDate >= blackoutEnd6)) {
g_form.clearValue('start_date');
g_form.clearValue('end_date');
g_form.showFieldMsg('start_date', 'Insufficient Lead time for your High/Critical Priority Change (2 days minimum, 13 days maximum)', 'error');
}
// 7th condition emergency priority SLA
else if ((state == '-5' || state == '-4') && type == 'emergency' && cabreject === 'false' && (newValueDate <= blackoutStart7 || newValueDate >= blackoutEnd7)) {
g_form.clearValue('start_date');
g_form.clearValue('end_date');
g_form.showFieldMsg('start_date', 'Insufficient Lead time for your Emergency Change (4 hours minimum, 2 days maximum)', 'error');
} else {
// Hide any existing error message if conditions are not met
g_form.hideFieldMsg('start_date', true);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2024 11:50 PM
Hi @tiguin2798 ,
Can you do the calculation in the script include and call it from the client script, because the official documentation of GlideDate() functionality in ServiceNow is defined in Server side Scripting.
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2024 11:47 AM - edited ‎11-08-2024 11:47 AM
Hello @Najmuddin Mohd and thank you for assisting with this! After going through the documentation and similar posts I tried to implement the below Script Includes with the following changes to the Client Script (I only changed condition 5 for testing purposes to call the script includes).
This does appear to be working in the default view, but unfortunately still does not work in the Service Operations Workspace view. Nothing happens sadly.
Do you know if maybe the way the date/time is getting pulled and formatted may differ in the SOW configuration? Or are there different API Objects I should use?
Default View When Low/Moderate Normal Change:
Script Includes:
var ChangeSLADates = Class.create();
ChangeSLADates.prototype = Object.extendsObject(AbstractAjaxProcessor, {
changeSLADates: function() {
var blackout5start = new GlideDateTime();
blackout5start.addDaysUTC(13);
return blackout5start.getDisplayValue(); // Return as a string
},
type: 'ChangeSLADates'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get values from the form
var state = g_form.getValue('state');
var type = g_form.getValue('type');
var priority = g_form.getValue('priority');
var critsystem = g_form.getValue('u_critical_systems');
var cabreject = g_form.getValue('u_cab_rejected');
var newValueDate = new Date(newValue);
// Define blackout periods
var blackoutStart1 = new Date('2024-11-28 00:00:00');
var blackoutEnd1 = new Date('2024-11-29 23:59:59');
var blackoutStart2 = new Date('2024-11-25 00:00:00');
var blackoutEnd2 = new Date('2024-11-27 23:59:59');
var blackoutStart3 = new Date('2024-12-16 00:00:00');
var blackoutEnd3 = new Date('2025-01-02 23:59:59');
var blackoutStart4 = new Date('2024-12-01 00:00:00');
var blackoutEnd4 = new Date('2024-12-15 23:59:59');
// Normal Change with low priority SLA (13 days out)
var ga = new GlideAjax('ChangeSLADates');
ga.addParam('sysparm_name', 'changeSLADates');
ga.getXMLAnswer(function(response) {
var blackoutStart5Str = response;
var blackoutStart5 = new Date(blackoutStart5Str);
var blackoutStart6 = new Date();
blackoutStart6.setDate(blackoutStart6.getDate() + 1);
var blackoutEnd6 = new Date();
blackoutEnd6.setDate(blackoutEnd6.getDate() + 13);
var blackoutStart7 = new Date();
blackoutStart7.setHours(blackoutStart7.getHours() + 4);
var blackoutEnd7 = new Date();
blackoutEnd7.setDate(blackoutEnd7.getDate() + 2);
// Condition 1: Nov 28-29th blackout condition
if ((state == '-5' || state == '-4') && newValueDate >= blackoutStart1 && newValueDate <= blackoutEnd1) {
showError('November 28th - November 29th is a change freeze period. No changes may be scheduled during this time.');
}
// Condition 2: Nov 25-27th low priority blackout condition
else if ((state == '-5' || state == '-4') && type == 'normal' && (priority == '4' || priority == '3') && newValueDate >= blackoutStart2 && newValueDate <= blackoutEnd2) {
showError('November 25th - November 27th is a change freeze period. Only High Priority or Emergency changes may be scheduled during this time.');
}
// Condition 3: Dec 16-Jan 2nd low priority blackout condition
else if ((state == '-5' || state == '-4') && type == 'normal' && (priority == '4' || priority == '3') && newValueDate >= blackoutStart3 && newValueDate <= blackoutEnd3) {
showError('December 16th - January 2nd is a change freeze period. Only High Priority or Emergency changes may be scheduled during this time.');
}
// Condition 4: Dec 1-15th if change has critical systems blackout condition
else if ((state == '-5' || state == '-4') && critsystem && newValueDate >= blackoutStart4 && newValueDate <= blackoutEnd4) {
showError('December 1st - December 15th is a change freeze period. No critical system changes may be scheduled during this time.');
}
// Condition 5: Low priority SLA (using fetched blackoutStart5)
else if ((state == '-5' || state == '-4') && type == 'normal' && (priority == '4' || priority == '3') && cabreject === 'false' && newValueDate <= blackoutStart5) {
showError('Insufficient Lead time for your Low/Moderate Change (14 days minimum)');
}
// Condition 6: High priority SLA
else if ((state == '-5' || state == '-4') && type == 'normal' && (priority == '1' || priority == '2') && cabreject === 'false' && (newValueDate <= blackoutStart6 || newValueDate >= blackoutEnd6)) {
showError('Insufficient Lead time for your High/Critical Priority Change (2 days minimum, 13 days maximum)');
}
// Condition 7: Emergency priority SLA
else if ((state == '-5' || state == '-4') && type == 'emergency' && cabreject === 'false' && (newValueDate <= blackoutStart7 || newValueDate >= blackoutEnd7)) {
showError('Insufficient Lead time for your Emergency Change (4 hours minimum, 2 days maximum)');
} else {
g_form.hideFieldMsg('start_date', true);
}
// Function to display error message
function showError(message) {
g_form.clearValue('start_date');
g_form.clearValue('end_date');
g_form.showFieldMsg('start_date', message, 'error');
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2024 03:28 AM
Hi @tiguin2798 ,
I believe it is due to UI Type Selected Desktop in the Client script.
Can you make UI Type to All and try it.
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-12-2024 10:20 AM
Hey @Najmuddin Mohd
Thank you for your response. Unfortunately, I do already have the UI Type set to "All" as well as have "isolate script" unchecked. I did look at my script includes and tried changing the scope from "this scope only" to "all scopes" but, it still did not resolve.
Do you have any other ideas?