To change the functionality described in the original server-side script to a client-side script for a ServiceNow catalog item and show a popup error if the functionality does not work, you would use an AJAX GlideRecord call. Note that in client scripts, direct GlideRecord queries are not permitted due to security reasons. You must use GlideAjax to make a call to a Script Include. Also, we will use g_form.addErrorMessage to display the error message as a popup.
Here's how you can do it:
Step 1: Create a Script Include
First, create a Script Include that will be called from the client script.
- Navigate to System Definition > Script Includes.
- Click New.
- Name it
CheckCIOperationalStatus and make sure it is Accessible from Client Scripts.
- Implement the code as follows:
var CheckCIOperationalStatus = Class.create();
CheckCIOperationalStatus.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkStatus: 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 any CI is operational
if (relGlide.parent.operational_status == 1) {
return "true";
}
}
return "false";
}
});
This script include will perform the server-side check and return "true" if any related CI is operational, otherwise "false".
Step 2: Create a Client Script
- Navigate to Service Catalog > Catalog Definitions > Catalog Items.
- Select the catalog item you want to add the client script to.
- Under
Related Links, click Catalog Client Scripts, then click New.
Here's an example client script (onChange or onLoad, based on your requirement):
function onLoad() {
// Assuming 'ip_address' is the variable name of the IP address field in the catalog item
var ipAddress = g_form.getValue('ip_address');
if (!ipAddress) {
return;
}
var ga = new GlideAjax('CheckCIOperationalStatus');
ga.addParam('sysparm_name', 'checkStatus');
ga.addParam('ip_address', ipAddress);
ga.getXMLAnswer(function(response) {
var vmFlag = (response === "true");
if (!vmFlag) {
// Show popup error message if no operational CI was found
g_form.addErrorMessage('No operational CI found for the given IP address. Please check the IP address and try again.');
}
});
}
Make sure to select the correct type (onChange, onLoad, etc.), field name, and control conditions as required for your client script.
These steps will ensure that when the catalog item is loaded or the IP address field is changed (depending on your setup), it checks for an operational CI with the given IP address in the background. If it doesn't find any, it shows a popup error message.