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

Jim Coyne
Kilo Patron

You are using the same variable to both query records and then insert new ones.

I think this post here is related to another of your posts at Insert items into list upon creation of time card


Mikolz
Kilo Contributor

Thanks for the response. I am assuming you are talking about the system_entry field? I also tried the BR without that query condition to see if I could pin point the problem, yet only one record was still being inserted. Even If I use an addActiveQuery, that doesnt seem to work either. And yes, the other post is similiarly related to this. This approach I just tried to cut out the 3 table factor and work with two. Even a bare bones GR like the following inserts one record and stops...



Default();

function Default()
{

var GG = new GlideRecord('u_m2m_time_card');
GG.addActiveQuery();
GG.query();
while(GG.next())
{
GG.initialize();
GG.insert();

}
current.update();
}


Jim Coyne
Kilo Patron

No, you are using the "GG" variable to both query records from "u_m2m_time_card" and then you initialize the same GlideRecord variable, so you are wiping it out and any records you might have returned in the query. If you look at my answer to the other post, it might get you closer to where you want to be.


gaidem
ServiceNow Employee
ServiceNow Employee

Just drop GG.initialize();