Simple way for duplicate a record

xif1
Mega Expert

Hello everyone,

 

Is it a way for duplicate a record easily ?

I means without copying each field ?

 

Actually I use this method

current.start_date = 'NULL';
current.end_date = 'NULL';
current.calendar_duration = 'NULL';
current.opened_at = current.opened_at;
current.opened_by = current.opened_by;
current.sys_created_on = current.sys_created_on;
current.sys_created_by = current.sys_created_by;
current.insert();

 

But my record have a lot of field, and It can be very long...

1 ACCEPTED SOLUTION

harikrish_v
Mega Guru

Hi Romain,



if you simply put current.insert(), it will insert a duplicate of the current record in your table. So incase you need to set certain values for this new record like:-



current.description="Duplicate record";


current.short_description="Duplicate";



you can set those values and then use current.insert().



Thanks & Regards,


Hari


View solution in original post

11 REPLIES 11

can you share the rest of the code if possible pls.


You've the entire piece of code.


But it seems to be cause by an other thing because now my ui action is not active


ok fine


marcguy
ServiceNow Employee
ServiceNow Employee

make sure there aren't any actions with duplicate action_name in that table....


Create a new System UI Action



Capture.PNGdoInsertAndStayRequest();
function doInsertAndStayRequest() {
var saveMe = current;
var orig_sys_id = current.getValue('sys_id');
var orig_number = current.getValue('number');
current.number = ""; // generate a new number
current.state = 1;
current.insert();
gs.eventQueue("task.insert", current, orig_sys_id, current.sys_id);
current.work_notes = "Cloned from Ticket: " + orig_number;
gs.addInfoMessage("Cloned from Ticket: " + orig_number);
Packages.com.glide.ui.SysAttachment.copy(current.getTableName(), orig_sys_id, current.getTableName(), current.sys_id);
current.update();
action.setRedirectURL(saveMe);
}