From 9a370e3677b12fa31605a7a8ca259f8661515426 Mon Sep 17 00:00:00 2001 From: Sumith Thota <108344062+SumithThota@users.noreply.github.com> Date: Tue, 21 Oct 2025 16:19:50 +0530 Subject: [PATCH] added a script that changes scope --- .../Background Scripts/update scope/README.md | 13 +++++++ .../Background Scripts/update scope/script.js | 37 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Server-Side Components/Background Scripts/update scope/README.md create mode 100644 Server-Side Components/Background Scripts/update scope/script.js diff --git a/Server-Side Components/Background Scripts/update scope/README.md b/Server-Side Components/Background Scripts/update scope/README.md new file mode 100644 index 0000000000..962455d843 --- /dev/null +++ b/Server-Side Components/Background Scripts/update scope/README.md @@ -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. + + diff --git a/Server-Side Components/Background Scripts/update scope/script.js b/Server-Side Components/Background Scripts/update scope/script.js new file mode 100644 index 0000000000..53a93bdabe --- /dev/null +++ b/Server-Side Components/Background Scripts/update scope/script.js @@ -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."); +})();