In servicenow, we have cmdb_rel_ci table in which there are 9 records and i need to update the parent.managed_by_group of the records. But i am not able to update it on one go because i want to update the managed_by_group of parent only. But parent records whenver i open is opening the record in other tables. How to update that group
Assistant
In ServiceNow, when you have Configuration Items (CIs) in the CMDB that are related through the table, updating a field like for the parent CIs can be a bit tricky since parent CIs can belong to various tables, not just one. However, there are a few ways to achieve your goal:
User
When i gliderecord on parent.sys_class_name then there is reference field named as parent which is open in another table and then i found managed by group
Assistant
cmdb_rel_ci
managed_by_group
Option 1: Script in a Background Script
You can use a background script to update the parent records. Here’s a basic example of how you might do this:
Navigate to Background Scripts:
Go to System Definition > Scripts - Background.
Run the Script:
You can use a script similar to the following:
javascript
(function() {
var relCIRecord = new GlideRecord('cmdb_rel_ci');
relCIRecord.query();
while (relCIRecord.next()) {
var parentSysId = relCIRecord.parent.sys_id;
var parentTable = relCIRecord.parent.sys_class_name; // Get the parent's table name
var parentCI = new GlideRecord(parentTable);
if (parentCI.get(parentSysId)) {
parentCI.managed_by_group = 'new_group_sys_id'; // Replace 'new_group_sys_id' with the actual sys_id of the group
parentCI.update();
}
}
})();
Option 2: Data Import and Transform Map
If you prefer not to write a script, you can use the Import Set and Transform Map capabilities:
Create an Import Set: Prepare a data sheet (such as an Excel file) with two columns: sys_id of the parent CIs and the managed_by_group.
Load Data into ServiceNow:
Navigate to System Import Sets > Load Data.
Load your data file.
Create a Transform Map:
Navigate to System Import Sets > Create Transform Map.
Define a transform map that targets the cmdb_ci table or the relevant table(s) of the parent records.
Map the sys_id and managed_by_group fields appropriately.
Run the Transform:
Transform the data to update the managed_by_group field of the parent CIs.
Notes
Backup Your Data: Before performing bulk updates, consider backing up your data to prevent accidental data loss.
Test in Sub-Production: Test your script or import set in a development or test instance before running it in production.
Access Rights: Ensure you have the necessary rights to modify the records and run scripts.
By using these methods, you should be able to update the managed_by_group field on the parent CI records efficiently.
When working with the cmdb_rel_ci table and trying to update fields in the parent CIs, such as managed_by_group, you'll often encounter records where the parent CI is in various tables. This is because the parent field in cmdb_rel_ci is a reference field that points to different CI classes in the CMDB.
To update the managed_by_group attribute for these parent CIs, you can follow these steps using a GlideRecord script. The key is to first identify the table (sys_class_name) where each parent CI resides, and then update the record in that table:
Step-by-Step Script
Access the Parent CI Using its Table and Sys ID: For each record, identify the class or table where the parent CI resides and then use that information to instantiate a GlideRecord for the parent CI.
Update the Managed By Group: Set the managed_by_group for the parent record.
Here’s a script example that demonstrates this approach:
javascript
(function() {
// Define the sys_id of the group you want to set for managed_by_group
var newManagedByGroupSysId = 'sys_id_of_new_group'; // Replace this with the actual sys_id of the group
// Create a GlideRecord for the cmdb_rel_ci table
var relCIRecord = new GlideRecord('cmdb_rel_ci');
relCIRecord.query();
while (relCIRecord.next()) {
// Get the parent sys_id and sys_class_name
var parentSysId = relCIRecord.getValue('parent');
var parentTable = relCIRecord.getValue('parent.sys_class_name');
if (parentTable && parentSysId) {
// Create a GlideRecord for the parent table
var parentRecord = new GlideRecord(parentTable);
// Get the parent record using its sys_id
if (parentRecord.get(parentSysId)) {
// Update the managed_by_group field
parentRecord.setValue('managed_by_group', newManagedByGroupSysId);
parentRecord.update();
gs.info('Updated managed_by_group for parent: ' + parentSysId + ' in table: ' + parentTable);
} else {
gs.warn('Parent record not found for sys_id: ' + parentSysId + ' in table: ' + parentTable);
}
} else {
gs.warn('Missing parent sys_class_name or sys_id for record in cmdb_rel_ci with sys_id: ' + relCIRecord.sys_id);
}
}
})();
Key Considerations
Permissions: Ensure you have the necessary permissions to update records in the CMDB and to execute background scripts.
Testing: Always test scripts in a development or test environment before applying them in production to prevent accidental data alteration.
Logging: Use logging (gs.info() and gs.warn()) to track script execution and identify any issues with missing data.
Conditioning Query: If you need to target specific relationships or CIs, add appropriate conditions to the relCIRecord.query().
By adopting this script and adjusting it to your specific needs, you should be able to update the managed_by_group for the parent CIs referenced in the cmdb_rel_ci table.