Creating a client script that achieves the functionality you described involves a few steps. Since client scripts run on the client side (the user's browser), they can't directly access the server-side GlideRecord API you used in your server-side script. However, you can use a GlideAjax call to execute server-side code from the client script and return the information you need.
Here's a breakdown of how to achieve your goal:
-
Write a Script Include: This will contain the server-side logic to check if there are any operational virtual servers based on the IP address provided. It will be callable from your client script using GlideAjax.
-
Create an OnChange Client Script: This will trigger whenever the "Select Server" field value changes. It will use GlideAjax to call the Script Include and process the response to show an alert and clear the value if necessary.
Step 1: Script Include
First, create a Script Include that’s accessible by client scripts.
Name: CheckOperationalServers
Client Callable: Checked (True)
Script:
var CheckOperationalServers = Class.create();
CheckOperationalServers.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isServerOperational: function() {
var ipAddress = this.getParameter('ip_address'); // Get the IP address passed from the client script
var relGlide = new GlideRecord('cmdb_rel_ci');
var queryString = "type=d93304fb0a0a0b78006081a72ef08444^child.ip_address=" + ipAddress;
relGlide.addEncodedQuery(queryString);
relGlide.query();
while (relGlide.next()) {
if (relGlide.parent.operational_status == 1) {
return "true"; // Return "true" as a string to indicate an operational server exists
}
}
return "false"; // Return "false" if no operational servers are found
}
});
Step 2: OnChange Client Script
Next, create an OnChange client script for the "Select Server" field.
Name: Whatever descriptive name you prefer
Table: The table where your catalog form resides
Field name: The exact field name of your "Select Server" field
Type: OnChange
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CheckOperationalServers');
ga.addParam('sysparm_name', 'isServerOperational');
ga.addParam('ip_address', newValue); // Assuming newValue holds the IP address. Adjust accordingly.
ga.getXMLAnswer(function(answer) {
if (answer == 'true') {
alert('An operational virtual server exists for this IP address. Please select a different server.');
g_form.clearValue('select_server_field_name'); // Replace 'select_server_field_name' with the exact field name
}
});
}
Remember to replace 'select_server_field_name' with the actual field name of the "Select Server" field and adjust the ‘ip_address’ parameter as needed based on how your catalog item passes the IP address (it might require custom logic based on your catalog item setup).
This setup will alert the user if any selected server is operational and clear the selection, providing the intended functionality.