- 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-25-2024 09:08 PM
something like this
Make the UI action client side
Client checkbox - true
Onclick function: cloneRecord()
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 arr = ['field1', 'field2', 'field3']; // give here the field names which you wish to copy
for (var i in arr)
gr[arr[i]] = current[arr[i]];
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:30 PM
Hi Ankur,
we need something where it copies all fields except sysid, created, created by etc by default, with the script you provided we need to update ui action whenever we add/remove fields which could lead to maintenance work.
thanks,
Rocky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-25-2024 10:02 PM
then do this
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.getFields();
for (var i = 0; i < fields.size(); i++) {
var field = fields.get(i);
var descriptor = field.getED();
var fieldName = descriptor.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-26-2024 07:50 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