onCellEdit Client Script to Restrict Change Dates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2024 08:32 AM
Hello,
I am trying to rewrite a business rule into an 'onCellEdit' client script on our change table for the below conditions as this rule is causing an error when creating changes from incident records.
Below is the client script set up and code, but it is not stopping changes from moving forward or throwing the error message when the change 'planned start date' is below 14 days. I am very new to java script still, can someone please assist with this code?
function onCellEdit(control, oldValue, newValue) {
// Ensure the script runs only for the correct field
if (control.getName() == 'planned_start_date') {
var changeModel = g_form.getValue('change_model');
var state = g_form.getValue('state');
var priority = g_form.getValue('priority');
// Check if conditions are met: normal change model, new state, and priority 3 or 4
if (changeModel == 'normal' && state == 'new' && (priority == 3 || priority == 4)) {
var plannedStartDate = newValue; // Get the new planned start date value
var today = new Date();
var targetDate = new Date(today);
targetDate.setDate(today.getDate() + 13); // 13 days from today
// Check if the new planned start date is not at least 13 days from now
if (new Date(plannedStartDate) < targetDate) {
g_form.showFieldMsg(control.getName(), 'Insufficient Lead time for your Change (14 days minimum)', 'error');
g_form.setValue(control.getName(), oldValue); // Revert to old value
return false; // Abort the update
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2024 09:06 AM
Hi @tiguin2798 Try below code
function onCellEdit(control, oldValue, newValue) {
if (control.getName() == 'planned_start_date') {
var changeModel = g_form.getValue('change_model');
var state = g_form.getValue('state');
var priority = g_form.getValue('priority');
var plannedStartDate = new Date(newValue);
if (isNaN(plannedStartDate.getTime())) {
g_form.showFieldMsg(control.getName(), 'Invalid date format', 'error');
g_form.setValue(control.getName(), oldValue); // Revert to old value
return false; // Abort the update
}
if (changeModel == 'normal' && state == 'new' && (priority == '3' || priority == '4')) {
var today = new Date();
var targetDate = new Date(today);
targetDate.setDate(today.getDate() + 13);
if (plannedStartDate < targetDate) {
g_form.showFieldMsg(control.getName(), 'Insufficient Lead time for your Change (14 days minimum)', 'error');
g_form.setValue(control.getName(), oldValue);
return false;
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2024 10:50 AM - edited 08-19-2024 10:52 AM
Hello, thank you for your suggestion! This worked for onCellEdit all but for the error message which did not show. I attempted to convert the error from the field message to "g_form.addErrorMessage" but this still did not show.
I did realize afterwards that this should be an onChange client script to achieve my goal when inside the record form of aborting updates to the change and planned start date field. I attempted to change this to an onChange client script and add focus to the field with the error message, but it is still not functioning as intended. Can you please assist?
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Ensure the script runs only for the correct field
if (control.getName() == 'planned_start_date') {
var changeModel = g_form.getValue('change_model');
var state = g_form.getValue('state');
var priority = g_form.getValue('priority');
// Check if conditions are met: normal change model, new state, and priority 3 or 4
if (changeModel == 'normal' && state == 'new' && (priority == 3 || priority == 4)) {
var plannedStartDate = newValue; // Get the new planned start date value
var today = new Date();
var targetDate = new Date(today);
targetDate.setDate(today.getDate() + 13); // 13 days from today
// Check if the new planned start date is not at least 13 days from now
if (new Date(plannedStartDate) < targetDate) {
g_form.showFieldMsg(control.getName(), 'Insufficient Lead time for your Change (14 days minimum)', 'error');
g_form.setValue(control.getName(), oldValue); // Revert to old value
g_form.setFocus('planned_start_date');
return false; // Abort the update
}
}
}
}