var relGlide = new GlideRecord("cmdbrelci); var queryString =…
- User
var relGlide = new GlideRecord("cmdb_rel_ci); var queryString = "type=d93304fb0a0a0b78006081a72ef08444^child.ip_address=" +current.variables.ip_address; relGlide.addEncodedQuery(queryString); relGlide.query(); workflow.scratchpad.vm_flag = false; while (relGlide.next()){ //if any CI is operational if(relGlide.parent.operational_status ==1){ workflow.scratchpad.vm_flag = true; } }
i have a reference field variable on my catalog item named selected server and refer to cmdb_ci_server table and a record is selected from the server table that linked to another record on cmdb_rel_ci table with the common field ip_address. i want that if i select a server on catalog then it will check all records related to the server on cmdb_rel_ci table and check all records operational status field. if the field is operational on cmdb_rel_ci table records then show a popup with the names of that records and clear the field value from catalog selected field
- Assistant
To achieve your goal of checking the operational status of records related to a selected server via its IP address, and then showing a popup with the names of those records if any is operational, while also clearing the field value from the catalog selected field, you will need to involve both front-end and back-end scripting. Since the requirement involves showing a popup and clearing field values on the form, which are actions performed on the client side, you'll need to use a combination of a client script for the ServiceNow catalog item and a server-side script (probably already encapsulated in your initial approach with
GlideRecordquerying).However, client scripts in ServiceNow don't directly support displaying popups with custom messages based on server-side calculations or queries due to the need for asynchronous server-client communication (usually handled via GlideAjax). Here's an outline of how to achieve your objectives:
1. Server-side Script
Your existing script that checks the operational status of related CI records is a good start. You might adapt this to be callable via a GlideAjax from a client script. Instead of setting a scratchpad value, you'll return the names of the operational CIs (if any).
2. GlideAjax to Call a Server-side Script
Implement a GlideAjax call to fetch operational CIs related to the selected server. This should be triggered when the "selected server" field changes.
3. Client Script
In the Service Catalog item, create a Client Script that is executed onChange of the "selected server" field. This script would use GlideAjax to call your server-side script, passing the IP address of the selected server.
Example Implementation
Server-side Script Include
Create a Script Include named
CheckOperationalCIsthat is client-callable. It will return the names of operational CIs related to the provided IP address.var CheckOperationalCIs = Class.create(); CheckOperationalCIs.prototype = Object.extendsObject(AbstractAjaxProcessor, { checkOperationalStatus: function() { var ip_address = this.getParameter('ip_address'); var operationalCIs = []; 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 any CI is operational if (relGlide.parent.operational_status == 1) { operationalCIs.push(relGlide.parent.getDisplayValue()); } } return JSON.stringify(operationalCIs); }, type: 'CheckOperationalCIs' });Client Script
Create an onChange Client Script for the "selected server" field.
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue === '') { return; } // Assuming 'selected_server' is the name of your reference field var serverIP = g_form.getValue('selected_server_ip_field'); // Ensure you have the correct field name for the IP address var ga = new GlideAjax('CheckOperationalCIs'); ga.addParam('sysparm_name', 'checkOperationalStatus'); ga.addParam('ip_address', serverIP); ga.getXMLAnswer(function(response){ var operationalCIs = JSON.parse(response); if (operationalCIs.length > 0) { var ciNames = operationalCIs.join('\n'); alert('The following CIs are operational and related to the selected server:\n' + ciNames); g_form.clearValue('selected_server'); // Clear the reference field } }); }Remember to replace placeholders such as
'selected_server_ip_field'with the actual field names and identifiers in your instance.Note
This solution assumes you have a way to get the
ip_addressof the selected server directly in the client script or that you pass the server's Sys ID to the Script Include to perform a lookup for the IP address therein. Adjust the field names and identifiers as per your actual implementation.