From 5187a02d488d6aff9889978a5a639e51258b5cda Mon Sep 17 00:00:00 2001 From: hanna-g-sn Date: Tue, 21 Oct 2025 10:53:56 +0100 Subject: [PATCH 1/3] Add README for CI selection validation script This README explains the validation of CI selections using GlideAjax in a client script, detailing its purpose, usage, and functionality. --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/README.md diff --git a/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/README.md b/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/README.md new file mode 100644 index 0000000000..e98d2a7bef --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/README.md @@ -0,0 +1,24 @@ +# Validate CI selection via GlideAjax with submit guard + +## What this solves +End users sometimes pick CIs that are retired or unsupported for fulfilment. This client script validates a selected CI using GlideAjax and prevents submission if the CI fails checks. +This could also be used where some CIs are supported by third parties and prevent use in incorrect forms. + +## Where to use +- Catalog Item with a reference variable to `cmdb_ci` +- Add the client script as an onChange on that variable +- Install the Script Include in the same scope + +## How it works +- onChange reads the selected CI sys_id +- Calls Script Include via GlideAjax +- Server checks for active CI and support group presence +- Client displays messages and disables submit if invalid + +## References +- GlideAjax + https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideAjax/concept/c_GlideAjaxAPI.html +- GlideForm client API + https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideForm/concept/c_GlideFormAPI.html +- GlideRecord + https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideRecord/concept/c_GlideRecordAPI.html From 3654c47c9fe0780baf60e91287641ad95de32316 Mon Sep 17 00:00:00 2001 From: hanna-g-sn Date: Tue, 21 Oct 2025 10:54:40 +0100 Subject: [PATCH 2/3] Implement CI validation on change event --- .../catalog_ci_validate_onchange.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/catalog_ci_validate_onchange.js diff --git a/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/catalog_ci_validate_onchange.js b/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/catalog_ci_validate_onchange.js new file mode 100644 index 0000000000..39f87c444a --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/catalog_ci_validate_onchange.js @@ -0,0 +1,31 @@ +function onChange(control, oldValue, newValue, isLoading) { + if (isLoading) return; + + var VAR_NAME = 'ci'; // replace with your CI variable name + + // Clear old messages and re-enable submit by default + g_form.clearMessages(); + if (g_form.setSubmitEnabled) g_form.setSubmitEnabled(true); + + var ciSysId = g_form.getValue(VAR_NAME); + if (!ciSysId) return; + + var ga = new GlideAjax('CIValidationAjax'); + ga.addParam('sysparm_name', 'validateCi'); + ga.addParam('sysparm_ci', ciSysId); + + ga.getXMLAnswer(function(answer) { + try { + var res = JSON.parse(answer || '{}'); + if (res.valid) { + g_form.addInfoMessage('CI is valid and supported: ' + (res.display || ciSysId)); + } else { + g_form.addErrorMessage(res.message || 'Selected CI is not valid for this request.'); + if (g_form.setSubmitEnabled) g_form.setSubmitEnabled(false); + } + } catch (e) { + g_form.addErrorMessage('Validation failed. Please try again or choose another CI.'); + if (g_form.setSubmitEnabled) g_form.setSubmitEnabled(false); + } + }); +} From f04f292dadcef922618ab1e6b945814e0e69f576 Mon Sep 17 00:00:00 2001 From: hanna-g-sn Date: Tue, 21 Oct 2025 10:55:26 +0100 Subject: [PATCH 3/3] Add CI validation via GlideAjax This script validates a Configuration Item (CI) selection via GlideAjax, checking if the CI is operational and has an assignment group. --- .../CIValidationAjax.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/CIValidationAjax.js diff --git a/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/CIValidationAjax.js b/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/CIValidationAjax.js new file mode 100644 index 0000000000..3d274b672f --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Validate CI selection via GlideAjax with submit guard/CIValidationAjax.js @@ -0,0 +1,37 @@ +var CIValidationAjax = Class.create(); +CIValidationAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { + validateCi: function() { + var ciSysId = this.getParameter('sysparm_ci'); + var result = { valid: false, display: '', message: '' }; + + if (!ciSysId) { + result.message = 'No CI provided.'; + return JSON.stringify(result); + } + + var ci = new GlideRecord('cmdb_ci'); + if (!ci.get(ciSysId)) { + result.message = 'CI not found.'; + return JSON.stringify(result); + } + + result.display = ci.getDisplayValue(); + + // Example rules: CI must be operational and have an assignment group + var isOperational = String(ci.install_status) === '1'; // installed + var hasGroup = !!ci.assignment_group; + + if (isOperational && hasGroup) { + result.valid = true; + return JSON.stringify(result); + } + + var reasons = []; + if (!isOperational) reasons.push('CI is not operational'); + if (!hasGroup) reasons.push('no assignment group is set'); + result.message = 'CI is not valid: ' + reasons.join('; ') + '.'; + return JSON.stringify(result); + }, + + isPublic: function() { return true; } +});