- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2024 08:20 PM
Hello Experts,
I need to build a ui action on custom application table, the expected functionality is when a user clicks on that ui action, a confirm box should be displayed and once clicked on ‘OK’, a duplicate record should be created with all fields copied from existing record and a new record should be opened in a new tab.
how can I achieve this, please help with the script.
thanks,
Rocky.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2024 08:56 PM
you can exclude number field as well and open in same tab
you can't open in new tab using server script
function duplicateRecord() {
var confirmValue = confirm("Are you sure you want to copy?");
if (confirmValue.toString() == 'true') {
gsftSubmit(null, g_form.getFormElement(), 'dup_rec');
}
}
if (typeof window == 'undefined')
insertClone();
function insertClone() {
gs.info('inside server side');
var gr = new GlideRecord(current.getTableName());
gr.initialize();
for (var key in current) {
if (!key.toString().startsWith('sys') && key.toString() != 'number')
gr[key] = current[key];
}
gr.insert();
action.setRedirectURL(gr);
}
To open in new tab you will have to use client side code and use GlideAjax and then open in new tab
Something like this
Script Include: Client callable
var InsertDuplicate = Class.create();
InsertDuplicate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
cloneRecord: function() {
var sysId = this.getParameter('sysparm_sysId');
var gr = new GlideRecord("tableName");
gr.addQuery("sys_id", sysId);
gr.query();
if (gr.next()) {
var newRecord = new GlideRecord(current.getTableName());
newRecord.initialize();
for (var key in current) {
if (!key.toString().startsWith('sys') && key.toString() != 'number')
newRecord[key] = gr[key];
}
var newRecordSysId = newRecord.insert();
return newRecordSysId;
}
},
type: 'InsertDuplicate'
});
UI Action code:
function duplicateRecord() {
var confirmValue = confirm("Are you sure you want to copy?");
if (confirmValue.toString() == 'true') {
var gaPhone = new GlideAjax('InsertDuplicate');
gaPhone.addParam('sysparm_name', 'cloneRecord');
gaPhone.addParam('sysparm_sysId', g_form.getUniqueValue());
gaPhone.getXMLAnswer(_handleResponse);
function _handleResponse(response) {
var answer = response;
var url = '/yourtableName?sys_id=' + answer; // give your table name here
g_navigation.open(url, '_blank');
}
}
}
I hope I have provided a thorough answer to your question. I'm confident that with your developer skills, you can take it further from here.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2024 08:39 PM
Hi @Ankur Bawiskar ,
Looks like it is going into server as I see the log we put exists in the logs. but I also receive the below warning in the logs.
Thanks,
Rocky.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2024 08:40 PM
Please use my updated script and it will work fine
function duplicateRecord() {
var confirmValue = confirm("Are you sure you want to copy?");
if (confirmValue.toString() == 'true') {
gsftSubmit(null, g_form.getFormElement(), 'dup_rec');
}
}
if (typeof window == 'undefined')
insertClone();
function insertClone() {
gs.info('inside server side');
var gr = new GlideRecord(current.getTableName());
gr.initialize();
for (var key in current) {
if (!key.toString().startsWith('sys'))
gr[key] = current[key];
}
gr.insert();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2024 09:13 PM
Hello,
You can create a UI Action with a confirm box, it would go something like this -
UI Action
if (confirm("Do you want to duplicate this record?")) {
var ga = new GlideAjax('DuplicateRecordScript');
ga.addParam('sysparm_name', 'duplicateRecord');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue()); // Current record sys_id
ga.getXMLAnswer(function(response) {
var newRecordSysId = response.responseXML.documentElement.getAttribute('answer');
if (newRecordSysId) {
// Open the new record in a new tab
var newTabUrl = "/incident.do?sys_id=" + newRecordSysId;
window.open(newTabUrl, '_blank');
} else {
alert("Failed to duplicate the record.");
}
});
Script include -
var DuplicateRecordScript = Class.create();
DuplicateRecordScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
duplicateRecord: function() {
var sysId = this.getParameter('sysparm_sys_id'); // Get the current record sys_id
var tableName = 'incident'; // Replace with your table name
var gr = new GlideRecord(tableName);
if (gr.get(sysId)) {
var newGr = new GlideRecord(tableName);
newGr.initialize();
// Copy all fields except system fields (e.g., sys_id, sys_created_by)
for (var fieldName in gr) {
if (gr.isValidField(fieldName) && !newGr.isMandatoryField(fieldName)) {
newGr[fieldName] = gr[fieldName];
}
}
// Insert the new record
var newSysId = newGr.insert();
// Return the sys_id of the new record
return newSysId;
}
return null; // Return null if record duplication failed
},
type: 'DuplicateRecordScript'
});
Let me know if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2024 01:13 AM
Hi @Rocky5 ,
Its very simple, do the following.
Create an UI Action and use below code.
Make UI action client callable.
function cloneOrCopyRecord() {
var confirmValue = confirm("Are you sure you want to copy?");
if (confirmValue) {
gsftSubmit(null, g_form.getFormElement(), 'copy_exiting_record');
}
}
if (typeof window == 'undefined')
insertExitingRecord();
function insertExitingRecord() {
var newRec = new GlideRecord(current.getTableName());
newRec.newRecord();
newRec.short_description = current.short_description;
newRec.description = current.description;
// Add additional fields as needed here
newRec.update();
action.setRedirectURL(newRec);
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------