How to clone a table

bonsai
Mega Sage

I want to duplicate a table with identical fields within the same instance.

You can use the same fields with an extended table, but you don't want the child table's records to appear in the parent table.

There are nearly 80 fields that I plan to create in the table, so it's tough to manually duplicate them.

Anyone know how to clone a table?

1 ACCEPTED SOLUTION

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @bonsai ,

You can use below script in background script 

The first argument is the source table, the table to be copied. The second argument is the new/target table. The third argument is Boolean: set it to false if you don't want to or need to create the indexes.

 

cpTable('incident', 'incident_copy', 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 TableDescriptor(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 + '');
        }
        tdNewTable.setFields(gr);
        tdNewTable.copyAttributes(td);
        tdNewTable.setRoles(td);
        tdNewTable.create();
        if (bCopyIndexes) {
            tdNewTable.copyIndexes(strOldTable, strNewTable);
        }
    }
}

 

Source :-https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0656519 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

3 REPLIES 3

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @bonsai ,

You can use below script in background script 

The first argument is the source table, the table to be copied. The second argument is the new/target table. The third argument is Boolean: set it to false if you don't want to or need to create the indexes.

 

cpTable('incident', 'incident_copy', 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 TableDescriptor(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 + '');
        }
        tdNewTable.setFields(gr);
        tdNewTable.copyAttributes(td);
        tdNewTable.setRoles(td);
        tdNewTable.create();
        if (bCopyIndexes) {
            tdNewTable.copyIndexes(strOldTable, strNewTable);
        }
    }
}

 

Source :-https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0656519 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

thank you!

I used the post-New York script from the page you provided and it worked!

When using an extended table, there was a bug that the label of the table became the label of the extended table, but it is minor!

matrixtushar
Tera Contributor

Hi,

 

The table should extend the Task table and once the table is created, there is no way we can change it.