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

Your script still matches exactly what is in that Wiki page. Are you only calling that small section of the script? If so, you aren't defining "oldid" or "newid" which would cause it to fail.



I tested the following in Helsinki for a user with only a couple incidents:



var str = "This is a new record";


var inc = new GlideRecord('incident');


        inc.addQuery('caller_id','c446d1e14f04020063d4650f0310c7c4');


        inc.query();


        while (inc.next()) {


                  inc.short_description = str;


                  inc.insert();


        }



It created a couple new records with all of the original incident values set, with the exception being the short_description field. inc.insert() seems to be working in this case.


I am passing the sys id as a function parameter and have checked the sys id as well to see if it is a valid record or not.