- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2025 04:54 PM
Hi,
I am new to scripting. I am trying to create a client script where if the user tries to select Begin and End date/time of an Outage outside the Change window will get an on-screen alert. Is there a simple way of achieving this without using Script Includes/Ajax etc?
Change window -
This is where user needs to see the alert if they update the outage dates -
It needs to be on change client script for each Begin and End fields.
You help will be much appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2025 05:12 PM
One way to handle this purely on the client is to compare the outage begin and end fields against the change window on every change of either field. A simple onChange client script can read the four values, construct GlideDateTime objects and then show a message when the outage is outside the change window.
- Use an onChange client script on both outage fields so that it fires whenever a user picks a date/time.
- Inside the script, get the change start and end date/time from the parent Change record (for example, the start_date and end_date fields).
- Convert all values to GlideDateTime objects so you can perform before/after comparisons.
- If the outage start is before the change start or the outage end is after the change end, call g_form.showFieldMsg to display an error message on the field.
Here is an example that you can attach to each outage date/time field:
var onChangeOutage = function(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === "") {
return;
}
// Read change window values (adjust field names as needed)
var changeStart = new GlideDateTime(g_form.getValue("start_date"));
var changeEnd = new GlideDateTime(g_form.getValue("end_date"));
var outageStart = new GlideDateTime(g_form.getValue("u_outage_begin"));
var outageEnd = new GlideDateTime(g_form.getValue("u_outage_end"));
// Only compare when all values are present
if (changeStart.nil() || changeEnd.nil() || outageStart.nil() || outageEnd.nil()) {
return;
}
if (outageStart.before(changeStart) || outageEnd.after(changeEnd)) {
var msg = "The outage window must fall within the change window ( " + changeStart.getDisplayValue() + " – " + changeEnd.getDisplayValue() + " )";
g_form.showFieldMsg(control.getName(), msg, "error");
} else {
// Clear any previous message
g_form.hideFieldMsg(control.getName());
}
};
Attach the function above as an onChange script on both the outage begin and end fields. It will alert the user immediately if the selected outage falls outside of the Change window without relying on any Script Includes or Ajax calls.
🔥 Was this answer useful? 👉 If so, click 👍 Helpful 👍 or Accept as Solution ✅ 💡🛠️🧠🙌
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2025 05:12 PM
One way to handle this purely on the client is to compare the outage begin and end fields against the change window on every change of either field. A simple onChange client script can read the four values, construct GlideDateTime objects and then show a message when the outage is outside the change window.
- Use an onChange client script on both outage fields so that it fires whenever a user picks a date/time.
- Inside the script, get the change start and end date/time from the parent Change record (for example, the start_date and end_date fields).
- Convert all values to GlideDateTime objects so you can perform before/after comparisons.
- If the outage start is before the change start or the outage end is after the change end, call g_form.showFieldMsg to display an error message on the field.
Here is an example that you can attach to each outage date/time field:
var onChangeOutage = function(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === "") {
return;
}
// Read change window values (adjust field names as needed)
var changeStart = new GlideDateTime(g_form.getValue("start_date"));
var changeEnd = new GlideDateTime(g_form.getValue("end_date"));
var outageStart = new GlideDateTime(g_form.getValue("u_outage_begin"));
var outageEnd = new GlideDateTime(g_form.getValue("u_outage_end"));
// Only compare when all values are present
if (changeStart.nil() || changeEnd.nil() || outageStart.nil() || outageEnd.nil()) {
return;
}
if (outageStart.before(changeStart) || outageEnd.after(changeEnd)) {
var msg = "The outage window must fall within the change window ( " + changeStart.getDisplayValue() + " – " + changeEnd.getDisplayValue() + " )";
g_form.showFieldMsg(control.getName(), msg, "error");
} else {
// Clear any previous message
g_form.hideFieldMsg(control.getName());
}
};
Attach the function above as an onChange script on both the outage begin and end fields. It will alert the user immediately if the selected outage falls outside of the Change window without relying on any Script Includes or Ajax calls.
🔥 Was this answer useful? 👉 If so, click 👍 Helpful 👍 or Accept as Solution ✅ 💡🛠️🧠🙌
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2025 09:35 PM
Thanks a lot for your response. I will try it and update. One last question, is there an advantage or drawback using Client script like this over a Script Include?