Tech Enthusiast.

Duplicate GlideRecord

A simple way to create a new gliderecord from another one.

Please, look at the number 5 if you use the autonumbering feature

function duplicateGlideRecord(tableName, sysID, fieldsToExclude) {
    var gr = new GlideRecord(tableName);
    gr.initialize();
    gr.get(sysID);
    gr.setValue('number', new NumberManager(tableName).getNextObjNumberPadded());
    if (typeof fieldsToExclude !== 'undefined')
        for (var i = 0; i < fieldsToExclude.length; i++)
            gr.setValue(fieldsToExclude[i], "NULL");
    return gr;
}

You’ll find some examples here below. The first one with only two parameters (tablename and sys_id of source gliderecord)

@example
var table = 'incident';
var sysID = 'ed92e8d173d023002728660c4cf6a7bc';
var clonedGR = duplicateGlideRecord(table, sysID);
clonedGR.insert();

The second example adds an array of fields. These ones must be excluded from duplication

var table = 'incident';
var sysID = 'ed92e8d173d023002728660c4cf6a7bc';
var fieldsToExclude = ['subcategory', 'contact_type'];
var clonedGR = duplicateGlideRecord(table, sysID, fieldsToExclude);
clonedGR.insert();