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; } +}); 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 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); + } + }); +}