in servicenow, in almhardware table configuration item number has been changed daily by…
- User
in servicenow, in alm_hardware table configuration item number has been changed daily by sccm and my requirement is that i want correct configuration item has been mapped to computer table and alm_hardware table. how to do this
- Assistant
To achieve the synchronization between the
alm_hardwaretable and thecmdb_ci_computertable in ServiceNow, especially when the Configuration Item (CI) numbers are being updated daily by SCCM, follow these steps:-
Understand the Relationships: Ensure that you understand how the
alm_hardware(Asset Management) table and thecmdb_ci_computer(Configuration Management) table are related. Typically, there is a reference to thecmdb_ci_computertable from thealm_hardwaretable via a field likecmdb_ci. -
Identify the Key Fields: Identify the key fields that are being updated daily. Typically, this would be the
asset_tagorserial_numberthat uniquely identifies the hardware in both tables. -
Update via Business Rule or Scheduled Script:
- Business Rule: You can create a Business Rule to update the reference when SCCM updates the
alm_hardwaretable.
Here’s an example of a Business Rule:
// Business Rule on alm_hardware table (function executeRule(current, previous /*null when async*/) { // Ensure the required field (e.g., asset_tag or serial_number) is not empty if (current.serial_number) { var grComputer = new GlideRecord('cmdb_ci_computer'); grComputer.addQuery('serial_number', current.serial_number); grComputer.query(); if (grComputer.next()) { // Map the found CI record to the current asset current.cmdb_ci = grComputer.sys_id; } else { // If no matching CI is found, clear the reference (optional) current.cmdb_ci = ''; } } else { // Log an error or handle the case where serial_number is empty gs.error('Serial number is empty for asset ' + current.sys_id); } })(current, previous);- Scheduled Script Execution: If you prefer to run a synchronization script at regular intervals, you can set up a Scheduled Script Execution.
Here’s an example of a Scheduled Job script:
// Script for Scheduled Job var grHardware = new GlideRecord('alm_hardware'); grHardware.query(); while (grHardware.next()) { if (grHardware.serial_number) { var grComputer = new GlideRecord('cmdb_ci_computer'); grComputer.addQuery('serial_number', grHardware.serial_number); grComputer.query(); if (grComputer.next()) { // Update the reference if a matching CI record is found grHardware.cmdb_ci = grComputer.sys_id; grHardware.update(); // Ensure you save the changes } else { // Handle cases where no matching CI is found (optional) grHardware.cmdb_ci = ''; grHardware.update(); } } } - Business Rule: You can create a Business Rule to update the reference when SCCM updates the
-
Incident Handling: Make sure to add error handling to notify if there are discrepancies or unmapped items that need manual review.
-
Testing and Validation: After implementing the solution, thoroughly test it to ensure that the correct mapping occurs and that there are no performance issues or errors.
-
Monitor and Adjust: After deployment, monitor the process and make any necessary adjustments based on feedback or new requirements.
By following these steps, you will ensure that the
alm_hardwaretable is correctly mapped to thecmdb_ci_computertable despite the daily updates from SCCM. -
- User
i have a requirement to update the cmdb_ci table cmdb_ci_computer table records by filtering alm_hardware table. Query the alm_hardware table with serial number is one of and then compare asset tag and configuration item fields, if both are same then do nothing and if different then update the cmdb_ci_computer and cmdb_ci table of that record with the asset tag number filled in both configuration item field and their name in cmdb_ci_computer table and cmdb_ci table
- Assistant
You can achieve this requirement by writing a script in ServiceNow. Here’s a detailed step-by-step guide and an example script to update the
cmdb_ci_computerandcmdb_citables based on thealm_hardwaretable:Steps to Achieve the Requirement:
- Retrieve Records from
alm_hardwareTable: Query thealm_hardwaretable to get records with serial numbers. - Fetch Corresponding Records in
cmdb_ci_computerTable: For each record inalm_hardware, find the matching record in thecmdb_ci_computertable using the serial number. - Compare and Update: Compare the
asset_tagandconfiguration_itemfields. Update if there is a discrepancy. - Update Fields: Update the
asset_tagandnamefields in thecmdb_ci_computerandcmdb_citables.
Example Script for Scheduled Job or Background Script:
// Step 1: Query the alm_hardware table var hardwareGR = new GlideRecord('alm_hardware'); hardwareGR.addNotNullQuery('serial_number'); hardwareGR.query(); while (hardwareGR.next()) { // Step 2: Fetch corresponding records in cmdb_ci_computer table using serial number var computerGR = new GlideRecord('cmdb_ci_computer'); computerGR.addQuery('serial_number', hardwareGR.serial_number); computerGR.query(); if (computerGR.next()) { // If corresponding CI record is found // Step 3: Compare asset_tag and configuration_item fields if ((computerGR.asset_tag != hardwareGR.asset_tag) || (computerGR.configuration_item != hardwareGR.cmdb_ci)) { // Step 4: Update fields in cmdb_ci_computer table computerGR.asset_tag = hardwareGR.asset_tag; computerGR.configuration_item = hardwareGR.cmdb_ci; // Assuming configuration_item is a column in cmdb_ci_computer computerGR.name = hardwareGR.asset_tag; // Updating the name if needed computerGR.update(); // Step 4: Update fields in the corresponding cmdb_ci record var cmdbGR = new GlideRecord('cmdb_ci'); cmdbGR.get(computerGR.sys_id); if (cmdbGR.isValidRecord()) { cmdbGR.asset_tag = hardwareGR.asset_tag; cmdbGR.name = hardwareGR.asset_tag; // Updating the name if needed cmdbGR.update(); } } } }Explanation:
-
Query the
alm_hardwareTable:var hardwareGR = new GlideRecord('alm_hardware'); hardwareGR.addNotNullQuery('serial_number'); hardwareGR.query(); -
Fetch Corresponding Records in
cmdb_ci_computerTable:var computerGR = new GlideRecord('cmdb_ci_computer'); computerGR.addQuery('serial_number', hardwareGR.serial_number); computerGR.query(); -
Compare and Update if Necessary:
if (computerGR.asset_tag != hardwareGR.asset_tag || computerGR.configuration_item != hardwareGR.cmdb_ci) { computerGR.asset_tag = hardwareGR.asset_tag; computerGR.configuration_item = hardwareGR.cmdb_ci; computerGR.name = hardwareGR.asset_tag; computerGR.update(); var cmdbGR = new GlideRecord('cmdb_ci'); cmdbGR.get(computerGR.sys_id); if (cmdbGR.isValidRecord()) { cmdbGR.asset_tag = hardwareGR.asset_tag; cmdbGR.name = hardwareGR.asset_tag; cmdbGR.update(); } }
Running the Script:
- Scheduled Job: Create a Scheduled Job and paste the script in the Job Definition.
- Background Script: Navigate to "System Definition" > "Scripts - Background" and run the script there for immediate execution.
Additional Considerations:
- Error Handling: Add robust error handling to manage unexpected scenarios.
- Performance: Ensure the script runs efficiently, especially if the number of records is large. Consider adding indexing on frequently queried fields like
serial_number.
By implementing this script, you'll achieve the synchronization between the
alm_hardware,cmdb_ci_computer, andcmdb_citables based on your specified criteria. - Retrieve Records from
- User
backend name of background script
- Assistant
In ServiceNow, "Background Scripts" are essentially ad-hoc scripts that administrators can run in order to manipulate data or perform administrative tasks. They are a powerful tool and should be used with caution because they can make changes to the database directly.
The backend name for Background Scripts in ServiceNow is
sys.scripts_background. This is the table in which the background scripts are stored.Accessing Background Scripts:
To access and run Background Scripts, follow these steps:
-
Navigate to Scripts - Background:
- Go to the Application Navigator and type "Scripts - Background".
- Click on the "Scripts - Background" module under the "System Definition" application.
-
Running a Script:
- In the "Run script" text area, you can write and execute JavaScript code.
- For example:
var count = 0; var grInc = new GlideRecord('incident'); grInc.addQuery('priority', '1'); grInc.query(); while (grInc.next()) { count++; } gs.print('Number of high priority incidents: ' + count);
Here is an example of how to use a background script based on your initial requirement of updating the
cmdb_ci_computerandcmdb_citables:// Query the alm_hardware table var hardwareGR = new GlideRecord('alm_hardware'); hardwareGR.addNotNullQuery('serial_number'); hardwareGR.query(); while (hardwareGR.next()) { // Fetch corresponding records in cmdb_ci_computer table using serial number var computerGR = new GlideRecord('cmdb_ci_computer'); computerGR.addQuery('serial_number', hardwareGR.serial_number); computerGR.query(); if (computerGR.next()) { // If corresponding CI record is found // Compare asset_tag and configuration_item fields if ((computerGR.asset_tag != hardwareGR.asset_tag) || (computerGR.configuration_item != hardwareGR.cmdb_ci)) { // Update fields in cmdb_ci_computer table computerGR.asset_tag = hardwareGR.asset_tag; computerGR.configuration_item = hardwareGR.cmdb_ci; // Assuming configuration_item is a column in cmdb_ci_computer computerGR.name = hardwareGR.asset_tag; // Updating the name if needed computerGR.update(); // Update fields in the corresponding cmdb_ci record var cmdbGR = new GlideRecord('cmdb_ci'); cmdbGR.get(computerGR.sys_id); if (cmdbGR.isValidRecord()) { cmdbGR.asset_tag = hardwareGR.asset_tag; cmdbGR.name = hardwareGR.asset_tag; // Updating the name if needed cmdbGR.update(); } } } }Points to Note:
- Authorization: Ensure that only authorized users have permission to run background scripts as they can manipulate the ServiceNow database directly.
- Testing: Always test your script in a non-production environment before running it in production.
- Backups: Consider taking a backup of the affected tables or records before running potentially destructive scripts.
- Optimization: Be cautious of the performance implications, especially if dealing with large datasets.
By using Background Scripts carefully, administrators can perform complex data manipulation and administrative tasks within ServiceNow.
-