Copy record into the same table with new sys_id

Abhishek52
ServiceNow Employee
ServiceNow Employee

I want to copy a record into the same table with different sys_id. Below is the code that I've written for this. Issue is even though retrieval is successful, canvas_name and feedbacks are not getting copied somehow and new record is getting generated with blank value of canvas name and feedback. Below is the screenshot of table structure and record list.

Script :

var caller = "321926301beb3300e5a2fc88cc4bcb04";

var sysparm_canvas = 2;
var gr = new GlideRecord("x_390488_retro_app_retro_feedback");
gr.addQuery("sys_id",caller);
gr.query();

if(gr.next()){

var feedback_type = gr.feedback_type;
var feedback = gr.feedback;
var canvas_name = gr.canvas_name;

gs.info (" feedback_type : "+feedback_type); // printing the record
gs.info (" feedback : "+feedback); // printing the record
gs.info (" canvas_name : "+canvas_name); // printing the record

gr.initialize();

gr.feedback= feedback; // not working
gr.canvas_name= canvas_name; // not working
gr.feedback_type= feedback_type; // working
gr.canvas_id = sysparm_canvas; // working

gr.insert();

}

Table Structure:

find_real_file.png

View List:

 

find_real_file.png

1 ACCEPTED SOLUTION

rajneeshbaranwa
Giga Guru

Can you try this ?

 

ar caller = "321926301beb3300e5a2fc88cc4bcb04";
var sysparm_canvas = 2;
var gr = new GlideRecord("x_390488_retro_app_retro_feedback");
gr.addQuery("sys_id",caller);
gr.query();

if(gr.next()){
//get value from gliderecord

}

 

var gr1 = new GlideRecord("x_390488_retro_app_retro_feedback");

gr1.initialize();

//set values

gr1.insert();

View solution in original post

5 REPLIES 5

schorsch
Tera Contributor

Short addition to the answer from Abhishek regarding using the same instance of GlideRecord. This is not working when you use a "while" loop instead of "if (gr.next(()", then you need to create a new instance of GlideRecord; e.g.

var getRec= new GlideRecord("<table x>");
getRec.addEncodedQuery= "<query to find the records they need to be copied>";
getRec.query();

while (getRec.next()){
var value1= getRec.val1;
var value2= getRec.val2;

var setRec= new GlideRecord("<table x>");

setRec.initialize();
setRec.val1= value1;
setRec.val2= value;
setRec.insert();
}