UI Page customisation help - mandating a field from a GlideDialogWindow

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2025 09:52 AM - edited 02-07-2025 09:53 AM
Hi Community,
I am required to mandate the Request type field when a Resource Plan gets extended or an extension is being requested of a Resource Plan.
When either of these UI Actions (Extend and Request Extension) are being used there is a pop up dialog window with 2 fields: New end date and Request type. OOTB only New end date is mandatory but I need for Request type to be as well.
I have been advised the behaviour is driven by the related UI pages: resource_new_end_date and request_for_resource_plan_extend.
I've started to work on the resource_new_end_date one:
//[instance].service-now.com/nav_to.do?uri=sys_ui_page.do?sys_id=549b8a5293c0320064f572edb67ffba6
I tried to replicate the behaviour from New end date, but it hasn't worked as expected.
I've modified the HTML (which works fine) and the Client Script which is the one I am having trouble with.
I managed to make it work up to the point that the extension would not submit unless there is a value, but when I tried to introduce an error message, that's when the functionality stopped working altogether:
HTML
<div class="modal-row">
<label class="col-md-4 col-lg-4 control-label form-group-bottom">
<span class="required-marker label_description"></span>
<span class="label-text">${gs.getMessage('Request type')}</span>
</label>
Client Script
- this has been added before validateValue() function
function validateRequestType() {
var error = false;
var requestType = trim(gel('request_type').value);
var requestTypeErrorBlock = $j('#request_type_error'); // Add an error block for request_type
if (requestType === "") {
requestTypeErrorBlock.html(getMessage('Select a valid request type'));
error = true;
} else
requestTypeErrorBlock.html(''); // Clear error message if validation passes
if (error) {
focusField('request_type');
return false; // Prevent submission if validation fails
}
return true;
}
- this is the attempt to amend the submitDialog(function) up to showLoadMask(); line
function submitDialog() {
// Validate fields before submitting the form
if (!validateDate()) {
return false;
}
if (!validateRequestType()) { // Validate request_type
return false;
}
if (gel('request_type').value !== "" && !validateValue()) {
return false;
}
Any advice on how I can make this work would be much appreciated!! 🙂
Have a nice weekend!!
Paula
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2025 10:04 PM
Hi @PaulaaO ,
try with this code
Here is your final corrected code with all the necessary fixes for validating the Request Type field and ensuring the error message displays properly.
function validateRequestType() {
var requestTypeField = gel('request_type');
var requestTypeErrorBlock = $j('#request_type_error');
if (!requestTypeField) {
console.error("Request Type field not found!");
return false;
}
var requestType = requestTypeField.value.trim();
if (requestType === "") {
requestTypeErrorBlock.text(getMessage('Select a valid request type'))
.css({ 'display': 'block', 'color': 'red' }); // Show error
focusField('request_type');
return false;
} else {
requestTypeErrorBlock.text('').css('display', 'none'); // Hide error
return true;
}
}
function submitDialog() {
if (!validateDate()) {
return false;
}
if (!validateRequestType()) { // Validate Request Type
return false;
}
if (gel('request_type').value !== "" && !validateValue()) {
return false;
}
showLoadMask(); // Proceed with submission
return true;
}
Ensure You Have This Error Block in HTML
<span id="request_type_error" class="error-message" style="display: none;"></span>
🔹 Fixes Applied:
✔ Ensured error block exists (id="request_type_error")
✔ Added null check for gel('request_type')
✔ Used .text() instead of .html() for error messages
✔ Styled the error message (display: block; color: red;)
✔ Cleared error message when the field is correctly filled
This will now correctly prevent submission if "Request Type" is empty while showing an error message.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 09:15 AM
Hi @Community Alums many thanks for your help with this. I have applied your code and it works up to the point of submission, as in the message gets displayed and the extension is prevented if there are no values in Request type, however it does not process if there is input into the fields. I get the following:
In case I missed something, I've attached the full Client Script from the UI page which includes the code you suggested, in case additional changes need to be made:
function validateDate() {
var endDateVal = trim(gel('end_date').value);
var endDateErrorBlock = $j('#endDateError');
var error = false;
var rangeErrorBlock = $j('#rangeError');
var endDateTime = getDateFromFormat(endDateVal, g_user_date_format);
if (isNaN(endDateTime) || endDateTime <= 0) {
endDateErrorBlock.html(getMessage('Select a valid end date'));
error = true;
} else
endDateErrorBlock.html('');
if (error) {
focusField('end_date');
return false;
}
var currentEndDate = $j('#current_end_date').val();
var currentStartDate = $j('#current_start_date').val();
var maxDuration = parseInt($j('#max_duration').val());
if (currentEndDate) {
var currentEndDateTime = getDateFromFormat(currentEndDate, g_user_date_format);
var cEndDate = new Date(currentEndDateTime);
cEndDate.setHours(0);
cEndDate.setMinutes(0);
cEndDate.setSeconds(0);
cEndDate.setMilliseconds(0);
var endDate = new Date(endDateTime);
endDate.setHours(0);
endDate.setMinutes(0);
endDate.setSeconds(0);
endDate.setMilliseconds(0);
var currentStartDateTime = getDateFromFormat(currentStartDate, g_user_date_format);
var cStartDate = new Date(currentStartDateTime);
cStartDate.setHours(0);
cStartDate.setMinutes(0);
cStartDate.setSeconds(0);
cStartDate.setMilliseconds(0);
if (cEndDate.getTime() > endDate.getTime()) {
rangeErrorBlock.html(new GwtMessage().getMessage('The new end date {0}, cannot be before the current end date {1}.', endDateVal, currentEndDate));
focusField('end_date');
return false;
} else if (cEndDate.getTime() == endDate.getTime()) {
rangeErrorBlock.html(new GwtMessage().getMessage('The new end date {0}, cannot be same as the current end date {1}.', endDateVal, currentEndDate));
focusField('end_date');
return false;
}
var timeDiff = Math.abs(endDate.getTime() - cStartDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)) + 1;
if (diffDays > maxDuration) {
rangeErrorBlock.html(new GwtMessage().getMessage('The duration of the resource plan is more than {0} which is the maximum number of days set in the resource properties', maxDuration + ""));
focusField('end_date');
return false;
}
}
return true;
}
function validateRequestType() {
var requestTypeField = gel('request_type');
var requestTypeErrorBlock = $j('#request_type_error');
if (!requestTypeField) {
console.error("Request Type field not found!");
return false;
}
var requestType = requestTypeField.value.trim();
if (requestType === "") {
requestTypeErrorBlock.text(getMessage('Select a valid request type'))
.css({
'display': 'block',
'color': 'red'
}); // Show error
focusField('request_type');
return false;
} else {
requestTypeErrorBlock.text('').css('display', 'none'); // Hide error
return true;
}
}
function validateValue() {
var error = false;
var requestedValue = trim(gel('requested_value').value);
var requestedErrorBlock = $j('#requested_value_error');
if (isNaN(requestedValue) || requestedValue <= 0) {
requestedErrorBlock.html(getMessage('Enter a valid value'));
error = true;
} else
requestedErrorBlock.html('');
if (error) {
focusField('requested_value');
return false;
}
return true;
}
function focusField(field) {
var e = gel(field);
if (e)
e.focus();
}
function submitDialog() {
if (!validateDate()) {
return false;
}
if (!validateRequestType()) { // Validate Request Type
return false;
}
if (gel('request_type').value !== "" && !validateValue()) {
return false;
}
showLoadMask(); // Proceed with submission
return true;
var glideDialogWindow;
if (gel('form_source').value != "workspace") {
glideDialogWindow = GlideDialogWindow.get();
}
var endDate = gel('end_date').value;
var value = gel('requested_value').value;
if (gel('form_source').value != "workspace") {
var planId = glideDialogWindow.getPreference('sysparm_plan_id');
var formSource = glideDialogWindow.getPreference('sysparam_form_source');
var onPromptComplete = glideDialogWindow.getPreference("onPromptComplete");
var rowData = glideDialogWindow.getPreference("rowData");
var flow = glideDialogWindow.getPreference("flow");
} else {
planId = gel('plan_id').value;
formSource = gel('form_source').value;
}
var requestTypeChoices = gel('sysChoice').value;
var requestMessage = JSON.parse(requestTypeChoices);
onExtendResourcePlan(planId, endDate, formSource, rowData, flow, requestType, value, requestMessage).then(function(result) {
if (formSource != "workspace") {
hideLoadMask();
glideDialogWindow.destroy();
if (formSource == 'grid') {
onPromptComplete.call(this, result.result, rowData, 'extendResourcePlan');
return;
}
if (formSource != 'modal')
window.location.reload(true);
else
window.location.href = 'blank.do?sysparm_returned_sysid=' + planId + '&sysparm_stack=clear';
} else {
iframeMsgHelper.confirm({
actionType: "ok",
status: "success"
});
}
}, function(error) {
console.error(error);
});
return false;
}
function cancelDialog() {
var c = gel('cancelled');
c.value = "true";
if (gel('form_source').value != "workspace")
GlideDialogWindow.get().destroy();
else
iframeMsgHelper.cancel({
actionType: "cancel"
});
return false;
}
function showLoadMask() {
var loadMask = document.getElementsByClassName('load_mask_container')[0];
loadMask.style.display = 'flex';
}
function onExtendResourcePlan(planId, endDate, formSource, rowData, flow, requestType, value, requestMessage) {
var data;
var config;
if (formSource == 'grid') {
rowData.endDate = endDate;
rowData.requestType = requestType;
rowData.value = value;
rowData.formSource = formSource;
rowData.requestMessage = requestMessage;
data = {
sysparm_action_name: 'extendResourcePlan',
sysparm_row_data: rowData,
sysparm_planner_flow: flow,
};
config = {
url: '/api/now/resource_grid/performAction',
method: 'post',
dataType: "json",
contentType: "application/json",
data: JSON.stringify(data),
"headers": {
"X-UserToken": window.parent.g_ck
}
};
} else {
data = {
planId: planId,
endDate: endDate,
formSource: formSource,
requestType: requestType,
requestedValue: value,
requestMessage: requestMessage
};
config = {
url: '/api/now/resource_plan/extendResourcePlan',
method: 'post',
dataType: "json",
contentType: "application/json",
data: JSON.stringify(data),
"headers": {
"X-UserToken": window.parent.g_ck
}
};
}
return jQuery.ajax(config);
}
function hideLoadMask() {
var loadMask = document.getElementsByClassName('load_mask_container')[0];
loadMask.style.display = 'none';
}