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

In servicenow, in alm_hardware table records show 5123 as stockroom that is currently…

  1. User

    In servicenow, in alm_hardware table records show 5123 as stockroom that is currently inactive and i need to update the stockroom reference field as the location of ci record attached to it . Give me a script to update the stockroom as ci location

  2. Assistant

    To update the alm_hardware records where the stockroom is set to an inactive stockroom (id 5123), with the location of the Configuration Item (CI) attached to each respective hardware record, you can use a background script in ServiceNow. Here's a script to accomplish this:

    1. Navigate to "System Definition" > "Scripts - Background" in your ServiceNow instance.
    2. Run the following script:
    (function() {
        var inactiveStockroomId = '5123'; // Stockroom ID that is inactive
    
        // GlideRecord for the alm_hardware table
        var hardwareGR = new GlideRecord('alm_hardware');
        hardwareGR.addQuery('stockroom', inactiveStockroomId); // Filter records with inactive stockroom
        hardwareGR.query();
    
        var count = 0; // Counter to keep track of updated records
    
        // Loop through each record in alm_hardware
        while (hardwareGR.next()) {
            if (hardwareGR.ci.location) {
                // Get the location from the CI (Configuration Item)
                var ciLocation = hardwareGR.ci.location;
    
                // Update the stockroom with the CI location
                hardwareGR.stockroom = ciLocation;
                hardwareGR.update();
                count++;
            } else {
                gs.warn("No location found for CI with sys_id: " + hardwareGR.ci.sys_id + " in hardware record: " + hardwareGR.sys_id);
            }
        }
    
        gs.print(count + " alm_hardware records updated with new stockroom locations.");
    })();
    

    Explanation:

    • inactiveStockroomId: Set the ID of the inactive stockroom.
    • Create a GlideRecord object (hardwareGR) for the alm_hardware table.
    • Query for records where the stockroom field matches the inactive stockroom ID.
    • Loop through the queried records:
      • Check if the CI (ci) attached to the hardware record has a location.
      • If a location exists, update the stockroom field with the CI's location.
      • Call the update() method to save changes to the record.
      • Count the number of records that have been updated.
      • If no location is found for a CI, it logs a warning with CI's sys_id.
    • Print the total count of updated records in the logs.

    Ensure you thoroughly test this script in a sub-production instance before executing it in production. Customizations and logic might vary based on your specific implementation and requirements.

  3. User

    Field seems to be empty and records is getting saved as it is mandatory field

  4. Assistant

    If the stockroom field is mandatory and an invalid value (or an attempt to clear it) causes the script to fail, you’ll need to handle that correctly. To ensure the script runs successfully and updates only the records where the required fields exist and are valid, you should include additional checks. Here's a revised script to address those issues:

    (function() {
        var inactiveStockroomId = '5123'; // Stockroom ID that is inactive
    
        // GlideRecord for the alm_hardware table
        var hardwareGR = new GlideRecord('alm_hardware');
        hardwareGR.addQuery('stockroom', inactiveStockroomId); // Filter records with inactive stockroom
        hardwareGR.query();
    
        var count = 0; // Counter to keep track of updated records
    
        // Loop through each record in alm_hardware
        while (hardwareGR.next()) {
            if (hardwareGR.ci && hardwareGR.ci.location) {
                // Get the location from the CI (Configuration Item)
                var ciLocation = hardwareGR.ci.location.sys_id;
    
                // Check if the ciLocation is valid
                if (ciLocation) {
                    // Update the stockroom with the CI location
                    hardwareGR.setValue('stockroom', ciLocation);
                    hardwareGR.update();
                    count++;
                } else {
                    gs.warn("Invalid location found for CI with sys_id: " + hardwareGR.ci.sys_id + " in hardware record: " + hardwareGR.sys_id);
                }
            } else {
                gs.warn("No location found for CI or CI is null in hardware record: " + hardwareGR.sys_id);
            }
        }
    
        gs.print(count + " alm_hardware records updated with new stockroom locations.");
    })();
    

    Explanation of Changes:

    1. Valid ci and location Checks: Added checks to ensure that the ci (Configuration Item) and its location fields are valid and not empty before attempting to update the stockroom.

    2. Sys_id Check: This ensures the sys_id of the CI location is valid before attempting to update the stockroom field.

    3. Error Handling: Added warnings to log for specific issues (e.g., no location or invalid location) for easier debugging.

    4. setValue Method: Used setValue('stockroom', ciLocation) for updating the stockroom reference field correctly with the CI's location sys_id.

    Usage:

    • Sub-Production Instance: Always test your scripts in a sub-production instance before running them in your production environment.
    • Validation: Validate the changes properly before and after running the script to ensure data integrity.

    This script should help you update the stockroom field only when valid location data is available, ensuring compliance with the mandatory field requirement and avoiding invalid updates.