Create a copy of a table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 11:39 AM
I have a custom table in a scoped application and need to create a copy of that table for history purpose. I tried to use the following code provided ServiceNow support but it creates the table in global scope. I need this table in custom scope. Any help is appreciated.
cpTable('sys_upgrade_history_log', 'u_testcase', true);
function cpTable(strOldTable, strNewTable, bCopyIndexes) {
var tu = new TableUtils(strNewTable);
var bNewTableAlreadyExists = tu.tableExists();
if (bNewTableAlreadyExists) {
gs.print("WARNING: Target Table " + strNewTable + " already exists! Please choose a new target table name");
} else {
var gr = new GlideRecord(strOldTable);
gr.initialize();
var td = GlideTableDescriptor.get(strOldTable);
var tdNewTable = new SNC.TableRotationBootstrap(strNewTable, gr.getLabel());
var dbo = new GlideRecord("sys_db_object");
dbo.addEncodedQuery("super_classISNOTEMPTY^name=" + strOldTable);
dbo.setLimit(1);
dbo.query();
if (dbo.next()) {
tdNewTable.setExtends(dbo.super_class.name + '');
}
tdNewTable.setFields(gr);
tdNewTable.copyAttributes(td);
tdNewTable.create();
if (bCopyIndexes) {
tdNewTable.copyIndexes(strOldTable, strNewTable);
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 11:44 AM
Hi,
Thread has a solution proposed by Mark. Have not tested in personally, but should help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 12:45 PM
I created a fix script in scoped application but its not able to access script include TableUtils which is in global scope. And I don't want to change to run from all scope as we are not allowed to do so.
Illegal access to private script include TableUtils in scope rhino.global being called from scope x_43436_workday: com.glide.script.system.ASystemInclude.exists(ASystemInclude.java:311)