To automate these tasks in ServiceNow, you can use a combination of a Business Rule and a script. Here's a general step-by-step process you can follow to meet your requirements:
Step 1: Create a Business Rule on the sc_req_item Table
- Navigate to
System Definition > Business Rules.
- Click on
New to create a new Business Rule.
Step 2: Define the Business Rule
Step 3: Script Section
You'll write a script to fetch the required task, get the IP address, and update the CMDB records.
(function() {
// Initialize variables
var provisionIp = '';
var targetGroupSysId = '82jwj8wj28299290dj';
// Query the sc_task table for the given RITM
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.sys_id); // Link tasks to the RITM
taskGR.query();
// Find any one task to get the provision_ip
if (taskGR.next()) {
provisionIp = taskGR.getValue('provision_ip');
}
if (!provisionIp) {
gs.warn('No provision IP found for RITM: ' + current.number);
return; // Exit if no IP found
}
// Query the cmdb_rel_ci table with your specific encoded query
var cmdbRelGR = new GlideRecord('cmdb_rel_ci');
cmdbRelGR.addEncodedQuery('<Your_Encoded_Query_Here>'); // Replace with actual query
cmdbRelGR.query();
// Update the child.ip_address and managed_by fields
while (cmdbRelGR.next()) {
// Open related records as per the requirement (assuming child references another table)
var childGR = new GlideRecord(cmdbRelGR.child.sys_class_name);
if (childGR.get(cmdbRelGR.child.sys_id)) {
childGR.setValue('ip_address', provisionIp); // Update IP address
childGR.setValue('managed_by_group', targetGroupSysId); // Update Managed by Group
childGR.update();
}
}
// Note: Consider adding error handling for missing records or fields if needed.
})();
Step 4: Testing
- Test the script thoroughly in a development or test instance. Verify that:
- It correctly identifies and updates tasks and CMDB relationships.
- It handles edge cases, such as missing IP addresses or empty query results.
Notes:
- Ensure appropriate permissions are in place to read and update the
sc_task and cmdb_rel_ci tables.
- Replace
<Your_Encoded_Query_Here> with the actual query you need to identify relevant cmdb_rel_ci records.
- Adjust field names (like
provision_ip, ip_address) according to your instance's specific field names or requirements.
- Consider logging actions (using
gs.info, gs.warn, etc.) for transparency and debugging.
- Ensure your script accounts for any additional logic or constraints pertinent to the environment or business process.