diff --git a/Specialized Areas/Fix scripts/Fix Corrupted Catalog Item References in RITMs/README.md b/Specialized Areas/Fix scripts/Fix Corrupted Catalog Item References in RITMs/README.md new file mode 100644 index 0000000000..ebe75536cb --- /dev/null +++ b/Specialized Areas/Fix scripts/Fix Corrupted Catalog Item References in RITMs/README.md @@ -0,0 +1,6 @@ +This script is designed to fix "Requested Items" (sc_req_item) records that have a missing catalog item (cat_item) reference. +The purpose of this script in ServiceNow is to correct data inconsistencies in the Requested Items (sc_req_item) table by +identifying and updating records that are missing a reference to their associated catalog item (cat_item). Specifically, it searches for RITM records where the cat_item field is null, +which typically indicates an incomplete or improperly imported record. For each of these, the script attempts to find a matching catalog item in the sc_cat_item table by comparing the RITM's short_description to the catalog item's name. +If a match is found, the script updates the RITM to reference the correct catalog item and keeps a count of how many records were fixed. This automated cleanup helps restore proper data relationships between requested items and their catalog definitions, +which is important for reporting, workflows, and overall data integrity in the ServiceNow instance. diff --git a/Specialized Areas/Fix scripts/Fix Corrupted Catalog Item References in RITMs/script.js b/Specialized Areas/Fix scripts/Fix Corrupted Catalog Item References in RITMs/script.js new file mode 100644 index 0000000000..3c3f50ea58 --- /dev/null +++ b/Specialized Areas/Fix scripts/Fix Corrupted Catalog Item References in RITMs/script.js @@ -0,0 +1,23 @@ +var ritm = new GlideRecord('sc_req_item'); +ritm.addNullQuery('cat_item'); // Missing reference +ritm.query(); + +var count = 0; +while (ritm.next()) { + var matchedItem = findCatItemByName(ritm.short_description); + if (matchedItem) { + ritm.cat_item = matchedItem.sys_id; + ritm.update(); + count++; + } +} + +function findCatItemByName(name) { + var item = new GlideRecord('sc_cat_item'); + item.addQuery('name', name); + item.setLimit(1); + item.query(); + return item.next() ? item : null; +} + +gs.info('Fixed RITMs with missing catalog item: ' + count);