Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

in servicenow, in almhardware table configuration item number has been changed daily by…

  1. 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

  2. Assistant

    To achieve the synchronization between the alm_hardware table and the cmdb_ci_computer table in ServiceNow, especially when the Configuration Item (CI) numbers are being updated daily by SCCM, follow these steps:

    1. Understand the Relationships: Ensure that you understand how the alm_hardware (Asset Management) table and the cmdb_ci_computer (Configuration Management) table are related. Typically, there is a reference to the cmdb_ci_computer table from the alm_hardware table via a field like cmdb_ci.

    2. Identify the Key Fields: Identify the key fields that are being updated daily. Typically, this would be the asset_tag or serial_number that uniquely identifies the hardware in both tables.

    3. Update via Business Rule or Scheduled Script:

      • Business Rule: You can create a Business Rule to update the reference when SCCM updates the alm_hardware table.

      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();
              }
          }
      }
      
    4. Incident Handling: Make sure to add error handling to notify if there are discrepancies or unmapped items that need manual review.

    5. 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.

    6. 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_hardware table is correctly mapped to the cmdb_ci_computer table despite the daily updates from SCCM.

  3. 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

  4. 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_computer and cmdb_ci tables based on the alm_hardware table:

    Steps to Achieve the Requirement:

    1. Retrieve Records from alm_hardware Table: Query the alm_hardware table to get records with serial numbers.
    2. Fetch Corresponding Records in cmdb_ci_computer Table: For each record in alm_hardware, find the matching record in the cmdb_ci_computer table using the serial number.
    3. Compare and Update: Compare the asset_tag and configuration_item fields. Update if there is a discrepancy.
    4. Update Fields: Update the asset_tag and name fields in the cmdb_ci_computer and cmdb_ci tables.

    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:

    1. Query the alm_hardware Table:

      var hardwareGR = new GlideRecord('alm_hardware');
      hardwareGR.addNotNullQuery('serial_number');
      hardwareGR.query();
      
    2. Fetch Corresponding Records in cmdb_ci_computer Table:

      var computerGR = new GlideRecord('cmdb_ci_computer');
      computerGR.addQuery('serial_number', hardwareGR.serial_number);
      computerGR.query();
      
    3. 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, and cmdb_ci tables based on your specified criteria.

  5. User

    backend name of background script

  6. 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:

    1. Navigate to Scripts - Background:

      • Go to the Application Navigator and type "Scripts - Background".
      • Click on the "Scripts - Background" module under the "System Definition" application.
    2. 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_computer and cmdb_ci tables:

    // 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.