Glide Record not inserting record

Mikolz
Kilo Contributor

Hey everyone, the below GR is behaving correctly. It should query a table and insert 5 new records. It is only inserting one. Not sure what is stopping it from going through all of them. Here is the code...


Default();

function Default()
{
var ID = current.sys_id.toString();

var GG = new GlideRecord('u_m2m_time_card');
GG.addQuery('u_system_entry', true);
GG.query();
while(GG.next())
{
GG.initialize();
GG.u_system_entry = false;
GG.u_time_card = ID;
GG.insert();
}
}


thoughts? Thanks.
8 REPLIES 8

geoffcox
Giga Guru

You are reinitializing GG inside the while loop. This blows away the list of GlideRecords you just got from your query. You must use a different variable:



function Default() {
var GG = new GlideRecord('u_m2m_time_card');
GG.addActiveQuery();
GG.query();
while(GG.next()) {
var new_time_card = new GlideRecord('u_m2m_time_card');
new_time_card.initialize();
new_time_card.u_system_entry = false;
new_time_card.u_time_card = ID;
new_time_card.insert();
}
}


gaidem
ServiceNow Employee
ServiceNow Employee

If you drop GG.initialize(); it will set the 2 values to what you want. Then insert a new record with all the other values equal to the record in the query; just like an insert and stay.


antonymirza
Tera Expert

Are you sure if your query returns five records.....


Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Mikolz,

Is this an "after" business rule? If "before" rule, should be using current.getUniqueValue() instead of current.sys_id.