How do you copy a GlideRecord from one extended table to another extended table?

Mike Moody
Kilo Guru

Here is my situation: I am trying to copy all the contents of one GlideRecord from a custom table that is extended from task to a different custom table also extended from task. All the fields I'm interested in copying reside on the task table. I'm trying to do this in such a way as to not have to explicitly copy each field in code. The problem I'm encountering is that once I copy the current record to the new GlideRecord, it takes on the target table of the current record.

Business Rule Code example:
var gr = new GlideRecord('u_tfc_snapshot');
gr = current;
gr.insert();

The above doesn't work because even though I instantiate the gr variable against the custom extended table it is overwritten by the table targeted on the current record. There is a getTableName() function which returns the GlideRecord's table name, however I cannot find an equivalent setTableName('u_tfc_snapshot') function that I can use before Inserting the record.

Any Ideas?

Thanks!

Mike

5 REPLIES 5

CapaJC
ServiceNow Employee
ServiceNow Employee

Try going the other direction. Start with the record you're copying, change the sys_class_name field, then do the insert. e.g.,



current.sys_class_name = "u_target_table";
current.insert();


Or maybe this:


var gr = current;
gr.sys_class_name = "u_target_table";
gr.insert();


Thanks for the response. I actually just recently tried the second approach you provided and it did not work. It still inserted the record into the "u_original_table" even after setting the sys_class_name value. No joy yet.

Another thing I tried was the following:

for (var propertyName in current) {
if (current[propertyName] != '' && current[propertyName] != null) {
gr3[propertyName] = current[propertyName];
}
}

The BR did not make it pass this for loop, probably invalid.

regards,

Mike


ssb
Tera Guru

That for loop is going to get all of the system properties as well... I'm not sure how you'd go trying to set those. If I were you I'd do two things:
1) add this to see how much the for loop executes: gs.addInfoMessage('Property: ' + propertyName);
2) add a check to ignore properties prefixed with 'sys_'


Mike Moody
Kilo Guru

I was able to achieve a solution via the following code:

var gr3 = new GlideRecord('u_tfc_snapshot');
gr3.initialize();
for (var propertyName in current) {
if (propertyName.toString().indexOf('sys_') > -1 || current[propertyName].isNil()) {
}
else {
gr3[propertyName] = current[propertyName];
}
}
gr3_sys_id = gr3.sys_id;
gr3.insert();