Create a copy of a table

Wirasat
Tera Guru

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);
        }
    }
}

  

2 REPLIES 2

Jaspal Singh
Mega Patron
Mega Patron

Hi,

Thread has a solution proposed by Mark. Have not tested in personally, but should help.

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)