- 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-27-2024 10:36 AM - edited 12-27-2024 10:44 AM
Hi @Ankur Bawiskar ,
I tried your script, but it says "Function getFields is not allowed in scope <table name>".
Is there any other function that replaces getfields for scoped app?
Also, I want the new record to open in new tab.
Thanks,
Rocky.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 11:36 AM
Hi @Ankur Bawiskar ,
I tried your script and it says 'Function getFields() will not work in custom scope' since I need this in custom scope for custom table, is there a alternative for getFields function?
also, once new record is inserted, I want the new record to be opened in new tab automatically.
Thanks,
Rocky.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2024 05:44 PM
use this -> use getElements() as it works in scoped app
function cloneRecord() {
var confirmValue = confirm("Are you sure you want to copy?");
if (confirmValue) {
gsftSubmit(null, g_form.getFormElement(), 'clone_record');
}
}
if (typeof window == 'undefined')
insertClone();
function insertClone() {
var gr = new GlideRecord(current.getTableName());
gr.initialize();
var fields = current.getElements();
for (var i = 0; i < fields.size(); i++) {
var field = fields.get(i);
var fieldName = field.getName();
// copy all fields except system
if (!fieldName.toString().startsWith('sys'))
gr[fieldName] = current[fieldName];
}
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:02 PM
Hi @Ankur Bawiskar ,
I tried getElements() too but no record is being created. below is the script I am using.
Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2024 08:13 PM
did you check it went to server side?
try this and debug and let us know
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader