From 6d7042861c401094f5a09f4ba5334157f74eda28 Mon Sep 17 00:00:00 2001 From: Willem Date: Thu, 9 Oct 2025 19:08:15 +0200 Subject: [PATCH 1/3] Update script.js - Adjusted the script to instead of not displaying the state field, flash the fields that are not filled in. This makes more sense and is using this existing function: https://www.servicenow.com/docs/bundle/zurich-api-reference/page/script/useful-scripts/reference/r_UsefulFieldScripts.html#d471717e187 - The getMissingFields() returns an array. Joining it with commas to improve readability of the info message. - To prevent further execution added return statement. --- .../script.js | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/script.js b/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/script.js index 197edcfb5e..17374f2384 100644 --- a/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/script.js +++ b/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/script.js @@ -1,19 +1,17 @@ function onChange(control, oldValue, newValue, isLoading, isTemplate) { - if (isLoading || newValue === '') { + if (isLoading || isTemplate || newValue === '') { return; } - //Client script, which shows how to use mandatoryCheck() function - //mandatoryCheck() allows validating if all mandatory fields are filled - //If all mandatory fields are filled it return true, otherwise it returns false - - //Check if all mandatory fields are filled on record + // Check if all mandatory fields are filled if (!g_form.mandatoryCheck()) { - - //Example action when not all mandatory fields are filled - display message and remove state field + //if not get all missing fields var missing = g_form.getMissingFields(); - g_form.addInfoMessage("State field removed, because not all mandatory fields are filled: " + missing); - g_form.setDisplay('state', false); + //Info message displaying the fields that are missing + g_form.addInfoMessage("Please complete the following mandatory fields: " + missing.join(", ")); + //go through missing fields and flash them + missing.forEach(function (fieldName) { + g_form.flash(fieldName, "#FFFACD",0); // Flash to draw attention + }); } - } From 59980d105f3ba627828604767d2e1dcf3b9472b9 Mon Sep 17 00:00:00 2001 From: Willem Date: Thu, 9 Oct 2025 19:26:55 +0200 Subject: [PATCH 2/3] Update README.md Updated the readme text. - Highlighting what the code provides. - Putting focus on the additional functionality of the error message that is provided by the function already, which was not mentioned in the original. Updated with 2 new screenshots --- .../README.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/README.md b/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/README.md index ea9a494c64..da8569c194 100644 --- a/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/README.md +++ b/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/README.md @@ -1,11 +1,17 @@ -**Client Script** +# Mandatory Field Check on Form Change -Client script which is showing how to use g_form.mandatoryCheck() function, which allows to easily detect if any of mandatory field is not filled on record. +This client script demonstrates how to use `g_form.mandatoryCheck()` to validate whether all mandatory fields on a form are filled. -**Example configuration** +It is typically used in an **onChange** catalog client script to trigger validation when a specific field changes. -![Coniguration](ScreenShot_1.PNG) +If mandatory fields are missing, the script: +- Displays an info message listing the missing fields. Note: the red message is the existing functionality that will give you an error message. +- Visually highlights each missing field using a flashing effect to guide the user. -**Example execution** +This approach improves user experience by clearly indicating which fields require attention. -![Execution](ScreenShot_2.PNG) +**Example configuration** +image + +**Example execution** +image From 7aadfdf2d43fb4b89ff8f7c39fd3187fd3f263ad Mon Sep 17 00:00:00 2001 From: Willem Date: Thu, 9 Oct 2025 19:28:59 +0200 Subject: [PATCH 3/3] Update script.js --- .../script.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/script.js b/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/script.js index 17374f2384..d83106348e 100644 --- a/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/script.js +++ b/Client-Side Components/Client Scripts/Check all mandatory fields using mandatoryCheck()/script.js @@ -1,7 +1,7 @@ function onChange(control, oldValue, newValue, isLoading, isTemplate) { - if (isLoading || isTemplate || newValue === '') { - return; - } + if (isLoading || newValue === '') { + return; + } // Check if all mandatory fields are filled if (!g_form.mandatoryCheck()) {