To accomplish the requirement you described, you will need to modify your workflow script slightly to ensure it checks the operation status of all related records in the cmdb_rel_ci table when a server is selected in your ServiceNow catalog item. Additionally, your goal is to show a popup message if any related record has an operational status of "operational", clear the selected server, and prevent form submission.
Your original script snippet begins the process of querying the cmdb_rel_ci table but doesn't complete the required operations (like checking the operational status, showing a popup, clearing the field value, and preventing submission).
Below is an adapted and more complete version of your script intended to be used as a Client Script (specifically as an "onChange" script for the "select_server" field). This example assumes the operational status you want to check is stored in a field called operational_status in the cmdb_rel_ci table and that you've set up a Reference Field on your catalog item to select a server (variable name select_server) which uses the cmdb_ci_server table.
Please note: Client Scripts used in Service Catalogs might have some limitations and might not directly support all operations exactly as you'd do on standard forms. If certain APIs or features (like g_form.clearValue) don't work as expected in the Service Catalog, consider using alternative methods or validating your requirements with server-side checks and delivering feedback messages via UI Policies or similar mechanisms.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Assuming 'select_server' is the name of your catalog item's reference variable
var serverIP = g_form.getValue('select_server');
// If no server is selected, exit the function
if (!serverIP) {
return;
}
// Use a GlideAjax call to perform server-side checks
var ga = new GlideAjax('YourServerSideScriptInclude');
ga.addParam('sysparm_name', 'checkOperationalStatus');
ga.addParam('sysparm_ip_address', serverIP);
ga.getXMLAnswer(function(answer) {
// 'answer' will be the response from the server-side script
var response = answer.toString();
// If the server-side script indicates a problem ("not_operational"), show an alert and clear the selection
if (response === 'not_operational') {
// NOTE: In Service Catalog, you might need to use alternative methods to show messages or clear selections.
alert("You cannot select this server because its parent record's operational status is operational.");
g_form.clearValue('select_server'); // Clear selected server
// Including this return statement won't actually stop form submission in an onChange client script.
// For actually blocking the submission, consider using a Catalog Client Script of type "onSubmit".
}
});
}
Additionally, you have to create a Script Include (YourServerSideScriptInclude in the code above) that does the server-side check. Here’s a basic example:
var YourServerSideScriptInclude = Class.create();
YourServerSideScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkOperationalStatus: function() {
var ip_address = this.getParameter('sysparm_ip_address');
var relGlide = new GlideRecord('cmdb_rel_ci');
var queryString = 'type=d93304fb0a0a0b78006081a72ef08444^child.ip_address=' + ip_address;
relGlide.addEncodedQuery(queryString);
relGlide.query();
while (relGlide.next()) {
if (relGlide.operational_status == 'operational') { // Assuming the field is named 'operational_status'
return 'not_operational'; // Indicate a record with operational status is found
}
}
return 'ok'; // Indicate all checks passed
},
type: 'YourServerSideScriptInclude'
});
Note: Make sure to replace placeholders like YourServerSideScriptInclude, operational_status, and other specifics based on actual field names and your requirements. Ensure to thoroughly test any customization in a safe, non-production environment to fine-tune and verify that it behaves as expected.