Updating the existing record instead of inserting new record

Kalaiarasan Pus
Giga Sage

I am using a set of statements which is exactly same as this

if (newid) {

  var crComp = new GlideRecord('u_m2m_companies_change_request');

  crComp.addQuery('u_change_request',oldid);

  crComp.query();

  while (crComp.next()) {

  crComp.u_change_request = newid;

  crComp.insert();

  }

}

Duplicate a Record Along with Related List - ServiceNow Wiki

The concept remains the same. I am using the outer gliderecord to fetch some existing records, changing a couple of fields in the record and inserting it.

But instead of inserting new records, the code is updating the existing records.

Is this behavior controlled by some property?

The exact same script works on our existing Eureka instance but not on the update Helsinki one!

6 REPLIES 6

Abhinay Erra
Giga Sage

Did you try adding   crComp.initialize();


I don't want to initialize. I am just inserting a duplicate copy of the record with only one field being changed. This is a exact replica of the wiki code as well.


Erik Stolberg
Tera Guru

It looks like you are using that script exactly. The creation of the new record happens at:


var newid = current.insert();



Have you tried logging that variable to see if it's getting created? Which records are getting updated instead of inserted-- the parent record, or one of the related list set of records?


The link might have confused you. Apologies for that.



var crComp = new GlideRecord('u_m2m_companies_change_request'); //table different


  crComp.addQuery('u_change_request',oldid); //query is different


  crComp.query();


  while (crComp.next()) {


  crComp.u_change_request = newid;


  crComp.insert();


  }



I am querying a existing record in a gliderecord, changing a field in the loop and inserting it. This, instead of creating a new record ends up updating the field alone (updating the existing record). Thus insert() statement seems to be working as a update().



I haven't done any change to this code and it was working fine for more than a year now. This has started acting this way since we upgrade to Helsinki recently.