Updating the existing record instead of inserting new record

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2016 12:20 PM
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!
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2016 12:28 PM
Did you try adding crComp.initialize();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2016 12:31 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2016 12:33 PM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2016 12:39 PM
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.