- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 07:27 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 08:11 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-08-2022 08:11 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-09-2022 12:46 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2024 12:35 AM
Hi,
The table should extend the Task table and once the table is created, there is no way we can change it.