Skip to content

Commit 9a370e3

Browse files
committed
added a script that changes scope
1 parent cdde65d commit 9a370e3

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Description:
2+
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.
3+
4+
## Configuration:
5+
6+
1. `oldScopeSysId` — the sys_id of the source (existing) application scope.
7+
Replace the placeholder string `SYS_ID_OF_THE_FIRST_SCOPED_APP` with the actual sys_id.
8+
2. `newScopeSysId` — the sys_id of the target (new) application scope.
9+
Replace the placeholder string `SYS_ID_OF_THE_NEW_SCOPED_APP` with the actual sys_id.
10+
Usage:
11+
3. Logs each successful update and failures if any catches exist.
12+
13+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(function() {
2+
// ----- CONFIGURATION -----
3+
var oldScopeSysId = "SYS_ID_OF_THE_FIRST_SCOPED_APP"; // Replace with old scope to update
4+
var newScopeSysId = "SYS_ID_OF_THE_NEW_SCOPED_APP"; // Replace with new scope to set
5+
6+
// ----- QUERY AND UPDATE -----
7+
var gr = new GlideRecord("sys_metadata");
8+
gr.addQuery("sys_scope", oldScopeSysId);
9+
gr.query();
10+
11+
if (!gr.hasNext()) {
12+
gs.info("No records found for scope: " + oldScopeSysId);
13+
return;
14+
}
15+
16+
while (gr.next()) {
17+
try {
18+
var oldValue = gr.sys_scope.toString();
19+
20+
// Set new scope
21+
gr.sys_scope = newScopeSysId;
22+
23+
// Avoid triggering workflows for system updates
24+
gr.setWorkflow(false);
25+
26+
// Update the record
27+
var updatedSysId = gr.update();
28+
29+
gs.info("Updated sys_metadata record " + updatedSysId +
30+
" from scope " + oldValue + " to " + newScopeSysId);
31+
} catch (e) {
32+
gs.error("Error updating record sys_id: " + gr.sys_id + " - " + e.message);
33+
}
34+
}
35+
36+
gs.info("Script completed: All relevant records updated.");
37+
})();

0 commit comments

Comments
 (0)