- 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:25 PM
update as this
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-29-2024 08:46 PM
Hi @Ankur Bawiskar ,
Looks like the below script you are providing is inserting a duplicate record, but it is also duplicating the record 'Number' field which shouldn't happen as it is not good to have duplicate numbers. Also, I need the newly inserted record be opened in new tab automatically.
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')) gr[key] = current[key]; } gr.insert(); }
- 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-30-2024 08:40 PM
Hope you are doing good.
Did my reply answer your question?
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-30-2024 08:42 PM
Hi Ankur,
I was able to achieve it. Also I was able to open new record in new tab from the server side and without script include as well. Thanks for you thorough help on this, appreciate your help.
thanks,
Rocky