To accomplish this task using a client script in a platform like ServiceNow, you would typically use a GlideAjax call to interact with the server-side code from the client side. This process involves creating a Client Script that makes a call to a Script Include. The Script Include will perform the server-side logic, like fetching records based on a condition (e.g., matching a child record's IP address) and then returning those records back to the client script.
Below is an example of how you can set this up. This example assumes you want to fetch all parent records that have at least one child record with a specific IP address and then check a certain field in each of those parent records.
Step 1: Create the Script Include
First, create a Script Include that will fetch the parent records based on the child's IP address. This Script Include is called from the client script using GlideAjax.
Script Include Example:
var FetchParentRecords = Class.create();
FetchParentRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Function to fetch parent records with a given child IP address
fetchByChildIP: function() {
var ip_address = this.getParameter('sysparm_ip_address'); // Get IP address from client script
var parentSysIds = []; // Array to hold sys_ids of matching parent records
// Query child table to find matching records by IP Address
var childRecord = new GlideRecord('child_table'); // Replace 'child_table' with your actual child table name
childRecord.addQuery('ip_address', ip_address); // Assuming 'ip_address' is the field name in child table
childRecord.query();
while (childRecord.next()) {
// Assuming 'parent' is the reference field in child table pointing to parent table
var parentId = childRecord.getValue('parent');
if (parentId && !parentSysIds.includes(parentId)) { // Avoid duplicates
parentSysIds.push(parentId);
}
}
return JSON.stringify(parentSysIds); // Return the sys_ids as JSON string
}
});
Step 2: Write the Client Script
This Client Script will use GlideAjax to call the Script Include and process the returned data.
Client Script Example (for a Form):
function fetchParentRecordsBasedOnChildIP(ip_address) {
var ga = new GlideAjax('FetchParentRecords'); // The name of your Script Include
ga.addParam('sysparm_name', 'fetchByChildIP'); // Function to call in Script Include
ga.addParam('sysparm_ip_address', ip_address); // Sending the IP Address to server
ga.getXMLAnswer(function(answer) {
var parentSysIds = JSON.parse(answer); // Parsing the returned sys_ids
if (parentSysIds.length > 0) {
// Looping through each sys_id to perform further actions
parentSysIds.forEach(function(sysId) {
// You can now use sysId to fetch more data or update records as needed
console.log('Parent Sys ID:', sysId); // Example action
// Note: If you need to do further GlideRecord operations here, you would need to make additional GlideAjax calls
});
} else {
alert('No matching parent records found.');
}
});
}
// Example usage
var childIPAddress = '192.168.1.1'; // Example IP address
fetchParentRecordsBasedOnChildIP(childIPAddress);
Level of Detail and Caution
This example assumes you're comfortable adjusting the logic to match your table and field names. When performing operations on large datasets, be mindful of performance implications. GlideAjax calls are asynchronous, so ensure any dependent logic accommodates this. Always test thoroughly in a sub-production instance before deploying to production.