
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2022 01:08 PM
Summary:
- Scoped application
- All business rules are disabled (to eliminate any possible issue)
- GlideRecord update() does not update the record (new record works)
- Anyone have a suggestion why the record is not updated?
Code:
try {
var sn_rec = new GlideRecord(MY_TABLE); sn_rec.addQuery('source_id', '=', json['source_id']); sn_rec.setLimit(1); sn_rec.query(); if (!sn_rec.next()) { sn_rec.initialize(); } // set field values for (var p in json) { sn_rec.setValue(p, json[p]); } // create or update sn_rec.update();
} catch(e) {
// no error when update fails
gs.error(e);
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2022 07:49 AM - edited ‎11-01-2022 07:51 AM
MYSTERY SOLVED!
I had some code that checked for duplicates and if so, create a copy, like this:
function checkForDuplicates(gr_original) { var gr = new GlideRecord(gr_original.getTableName()); gr.addQuery('source_id', '=', source_id); gr.query(); while (gr.next()) { if (duplicate) { // change some fields and insert a new record gr.insert(); } } }
I incorrectly assumed that gr.insert() would create a record and not change gr_original. I changed the code to this and all works well.
function checkForDuplicates(gr_original) { var orig_sys_id = gr_original.getUniqueValue(); var gr = new GlideRecord(gr_original.getTableName()); gr.addQuery('source_id', '=', source_id); gr.query(); while (gr.next()) { if (duplicate) { // change some fields and insert a new record gr.insert(); } } // restore original record gr_original.get(orig_sys_id); }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2022 04:09 AM
Thanks for the suggestion. There seems to be some fundamental problem with this particular table. Other tables are updating just fine on my PDI from background scripts.
Another curious observation is that if I have one of the records open in the UI, run the background script to modify, I see that some fields are marked as changed in the UI (the ones I expected to change). If I then reload form or page refresh, the record shows the old data. View XML also shows the old data.
Oh, and one other curious observation. If I add a brand new column to the table, run the background script, the record is updated.
And yes, the JSON data has new information. This isn't a case of no update due to no change. There is most definitely a change in the values.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2022 07:49 AM - edited ‎11-01-2022 07:51 AM
MYSTERY SOLVED!
I had some code that checked for duplicates and if so, create a copy, like this:
function checkForDuplicates(gr_original) { var gr = new GlideRecord(gr_original.getTableName()); gr.addQuery('source_id', '=', source_id); gr.query(); while (gr.next()) { if (duplicate) { // change some fields and insert a new record gr.insert(); } } }
I incorrectly assumed that gr.insert() would create a record and not change gr_original. I changed the code to this and all works well.
function checkForDuplicates(gr_original) { var orig_sys_id = gr_original.getUniqueValue(); var gr = new GlideRecord(gr_original.getTableName()); gr.addQuery('source_id', '=', source_id); gr.query(); while (gr.next()) { if (duplicate) { // change some fields and insert a new record gr.insert(); } } // restore original record gr_original.get(orig_sys_id); }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2025 10:17 AM - edited ‎06-06-2025 01:12 PM
I recently ran into a strange bug where I:
1) Fetched a GlideRecord in Function A,
2) Passed it by reference to Function B, which then passed it by reference to Function C.
3) In Function C, updated a field value.
4) Called gr.update().
The return value from gr.update() was consistently null - and the field value was not updated in the record.
After fussing with this for a long time, I finally tried re-fetching the GlideRecord in Function C before calling gr.update(). That solved the immediate problem.
The root cause was this: IF you invoke gr.update() on your GlideRecord (perhaps in a called function) after initially fetching it, it is NO LONGER VALID for purposes of updating. gr.isValidRecord() still returns true, but a second gr.update() call will always fail and return null.
JavaScript
crGr.get(crGr.sys_id); // this is necessary for the update() to succeed.
crGr.setValue("u_rework_state", new_rework_state);
var status = crGr.update();