Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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);
Loading