If the related tables vary for each record in the cmdb_rel_ci table, you'll need to implement a mechanism to handle this variability dynamically. You can achieve this by identifying the table dynamically for each record. This might involve using a reference field that points to different tables based on the sys_class_name field or similar logic.
Here's a strategy to dynamically handle related tables:
- Identify each record's target table.
- Update each related record's "Managed by Group" field in the identified table.
Here is a conceptual script to achieve the dynamic handling:
// Retrieve the RITM to work with.
var ritmSysId = current.sys_id.toString();
// Step 1: Query the 'sc_task' table for a task related to this RITM
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', ritmSysId);
taskGr.query();
if (taskGr.next()) {
// Step 2: Get the 'Provision IP' field value
var provisionIp = taskGr.getValue('provision_ip');
// Step 3: Query the 'cmdb_rel_ci' table to update or insert the record
var cmdbRelCiGr = new GlideRecord('cmdb_rel_ci');
cmdbRelCiGr.addQuery('child.ip_address', provisionIp);
cmdbRelCiGr.query();
if (!cmdbRelCiGr.next()) {
cmdbRelCiGr.initialize();
cmdbRelCiGr.child_ip_address = provisionIp;
cmdbRelCiGr.insert();
} else {
cmdbRelCiGr.child_ip_address = provisionIp;
cmdbRelCiGr.update();
}
// Step 4: Query related records and update based on different related tables
var relCiGr = new GlideRecord('cmdb_rel_ci');
relCiGr.addEncodedQuery('<YOUR_ENCODED_QUERY_HERE>');
relCiGr.query();
var managedByGroupSysId = '82jwj8wj28299290dj';
while (relCiGr.next()) {
var className = relCiGr.getValue('child.sys_class_name'); // or another field indicating the table name
if (!className) {
// Handle or log this scenario if there's no class name.
continue;
}
// Dynamically access the table based on className
var relatedGr = new GlideRecord(className);
relatedGr.get(relCiGr.child); // Assume 'child' is the relevant reference field
if (relatedGr.isValidRecord()) {
relatedGr.setValue('managed_by_group', managedByGroupSysId);
relatedGr.update();
}
}
}
Important Steps and Considerations:
-
Dynamic Class Name Resolution: The script assumes there is a way to dynamically determine the table name for each 'cmdb_rel_ci' entry, possibly via a sys_class_name or equivalent field.
-
Flexible References: The script uses a flexible querying mechanism where relatedGr.get(relCiGr.child) is used, assuming child references the correct record in its table. Adjust this logic if your actual data model differs.
-
Testing: Always thoroughly test in a development or test environment before deploying to production to ensure the script functions as expected and does not cause unintended modifications.
-
Permissions: Ensure that invoking user roles and permissions allow reading from and writing to the various tables.
-
Logging and Error Handling: Implement appropriate logging and error handling as necessary to monitor the workflow for debugging and auditing purposes.