In servicenow, in alm_hardware table records show 5123 as stockroom that is currently…
- 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
- Assistant
To update the
alm_hardwarerecords where the stockroom is set to an inactive stockroom (id5123), 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:- Navigate to "System Definition" > "Scripts - Background" in your ServiceNow instance.
- 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
GlideRecordobject (hardwareGR) for thealm_hardwaretable. - Query for records where the
stockroomfield 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
stockroomfield 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.
- Check if the CI (
- 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.
- User
Field seems to be empty and records is getting saved as it is mandatory field
- Assistant
If the
stockroomfield 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:
-
Valid
ciandlocationChecks: Added checks to ensure that theci(Configuration Item) and itslocationfields are valid and not empty before attempting to update thestockroom. -
Sys_id Check: This ensures the
sys_idof the CI location is valid before attempting to update thestockroomfield. -
Error Handling: Added warnings to log for specific issues (e.g., no location or invalid location) for easier debugging.
-
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
stockroomfield only when valid location data is available, ensuring compliance with the mandatory field requirement and avoiding invalid updates. -