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

marcguy
ServiceNow Employee
ServiceNow Employee

I use a slightly different version of the insert UI Action that is out of the box, just to create a record with a different number of ID




UI Action


Insert


Action Name: sysverb_insert


Script:


if (typeof current.number != 'undefined' && current.number) {


    current.number = ""; // generate a new number


}


answer = current.insert();


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


Thank you mguy and Harikrishnan for your quick answer, it's perfect


I've a little error cause maybe by the id, even if i put an other current.number...




Unique Key violation detected by database (Duplicate entry '0a94ab0621c6210074b7e729310d4b7b' for key 'PRIMARY')



function duplicateAndConvert()


{


current.insert();


  action.setRedirectURL(current);


}