Providing a direct script to fulfill your precise requirements might involve very specific details about your ServiceNow instance and its configurations, but I can guide you through the logic and suggest a possible approach using client scripts (for the popup) and a Scripted REST API or server script (for the check).
Step 1: Client Script to Capture Change in the Reference Field
Firstly, you'll need a client script to monitor changes in the "select_server" reference field. This script will invoke a script include or a Scripted REST API to perform your desired checks. Here's a basic outline for this client script:
// Type: onChange
// Field Name: select_server
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (!isLoading && newValue) {
var serverSysID = newValue;
// Call your Script Include or REST API, passing the serverSysID
// Assuming a REST API is used:
var serverURL = "/api/your_namespace/check_parents_operational_status?sys_id=" + serverSysID;
// Making a server call
var request = new XMLHttpRequest();
request.open('GET', serverURL, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
// Success!
var resp = this.response;
// Assuming the response will be a simple true or false
if (resp == 'true') {
// Popup if true
alert('All parent records have operational status 1');
}
// If false, no action required as per requirements
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
}
}
Step 2: Scripted REST API or Server Script for the Check
The server-side logic can either be implemented within a Script Include that is called from your client script or through a Scripted REST API. Given the modern approach in ServiceNow, let's use a Scripted REST API for this. You will have to adjust the logic based on the exact schema and relationships in your instance:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var serverSysID = request.queryParams.sys_id;
var answer = false;
// Logic to fetch child record (assuming you have already this record's sys_id)
var childGR = new GlideRecord('cmdb_ci_server');
if (childGR.get(serverSysID)) {
var ipAddress = childGR.ip_address;
// Now, find all parent records linked through the cmdb_rel_ci table
var parentGR = new GlideRecord('cmdb_rel_ci');
parentGR.addQuery('child.ip_address', '=', ipAddress);
parentGR.query();
while (parentGR.next()) {
// Assuming 'operational_status' is a field in the related parent table and '1' denotes the desired status
// This also assumes that you need to check a field on the parent record directly associated with 'cmdb_rel_ci' record
var parent = new GlideRecord('parent_table'); // Replace 'parent_table' with actual parent table name
if (parent.get(parentGR.parent.sys_id) && parent.operational_status == '1') {
answer = true;
} else {
answer = false;
break; // If even one parent doesn't satisfy the condition, break early
}
}
}
// Return the result back to the client script
return answer.toString();
})(request, response);
Important Considerations:
- Security: Ensure you're considering who can execute these scripts and access this data. Secure the Scripted REST API accordingly.
- Performance: The logic, as outlined above, can be resource-intensive, particularly if there are many related records. Consider optimizing your queries and logic to be as efficient as possible.
- Table Relationships: This solution presumes a certain schema (in terms of relationships between tables) which might not exactly match your setup. Adjust the queries to match your actual schema.
- GlideRecord vs AJAX: Using
XMLHttpRequest (AJAX) in ServiceNow client scripts is generally discouraged in favor of using GlideAjax to call Script Includes. However, for modern web projects and especially when integrating with external systems or leveraging Scripted REST APIs, it might still be relevant.
This approach involves both client-side and server-side scripting, which should be tested carefully in a development instance before moving to production. Always check for the latest best practices and follow your organization's development guidelines.