The CreatorCon Call for Content is officially open! Get started here.

UI Action to duplicate the existing record

Rocky5
Kilo Sage

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. 

1 ACCEPTED SOLUTION

@Rocky5 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

18 REPLIES 18

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.

 

Rocky5_0-1735533569741.png

 

 

Thanks,

Rocky.

@Rocky5 

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.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Omkar Mone
Mega Sage

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.

 

Runjay Patel
Giga Sage

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);

}

RunjayPatel_0-1735204334154.png

RunjayPatel_1-1735204367688.png

 

-------------------------------------------------------------------------

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

-------------------------------------------------------------------------

 

 

 

Part 4. In this video i have talked about ServiceNow form, list, menu, sub-menu, application, table and more basic thing which are necessary to know. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on upcoming videos: ...