Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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; }
});
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
Loading