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
13 changes: 13 additions & 0 deletions Server-Side Components/Background Scripts/update scope/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Description:
This background script updates records in the ServiceNow sys_metadata table by changing their sys_scope value from one application scope to another. Use this script when you need to reassign metadata artifacts (for example during migrations or scope consolidation) from an old scoped application to a new one. Run it only in a development or test instance first.

## Configuration:

1. `oldScopeSysId` — the sys_id of the source (existing) application scope.
Replace the placeholder string `SYS_ID_OF_THE_FIRST_SCOPED_APP` with the actual sys_id.
2. `newScopeSysId` — the sys_id of the target (new) application scope.
Replace the placeholder string `SYS_ID_OF_THE_NEW_SCOPED_APP` with the actual sys_id.
Usage:
3. Logs each successful update and failures if any catches exist.


37 changes: 37 additions & 0 deletions Server-Side Components/Background Scripts/update scope/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(function() {
// ----- CONFIGURATION -----
var oldScopeSysId = "SYS_ID_OF_THE_FIRST_SCOPED_APP"; // Replace with old scope to update
var newScopeSysId = "SYS_ID_OF_THE_NEW_SCOPED_APP"; // Replace with new scope to set

// ----- QUERY AND UPDATE -----
var gr = new GlideRecord("sys_metadata");
gr.addQuery("sys_scope", oldScopeSysId);
gr.query();

if (!gr.hasNext()) {
gs.info("No records found for scope: " + oldScopeSysId);
return;
}

while (gr.next()) {
try {
var oldValue = gr.sys_scope.toString();

// Set new scope
gr.sys_scope = newScopeSysId;

// Avoid triggering workflows for system updates
gr.setWorkflow(false);

// Update the record
var updatedSysId = gr.update();

gs.info("Updated sys_metadata record " + updatedSysId +
" from scope " + oldValue + " to " + newScopeSysId);
} catch (e) {
gs.error("Error updating record sys_id: " + gr.sys_id + " - " + e.message);
}
}

gs.info("Script completed: All relevant records updated.");
})();
Loading