
- 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
‎10-31-2022 01:50 PM
Do you have an example of what the JSON object looks like?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2022 04:00 AM
The JSON is just like the table except without sys_ columns. For example, if the table has a column "name" the JSON has "name".

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2022 06:19 AM - edited ‎11-01-2022 06:20 AM
Actually, this got me thinking that maybe a JSON value length greater than the table column size might cause the record to not be updated. I tried this, but still update fails:
// there are lots of fields, this is an example var FIELD_MAP = { name: 255, password: 255, description: 1000,
database_instance_ref: 0 }; for (var p in FIELD_MAP) { if (FIELD_MAP[p]) { sn_rec.setValue(p, (json[p] || '').substring(0, FIELD_MAP[p])); } else { sn_rec.setValue(p, json[p]); } }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2022 01:55 PM
To prove your point on a background script try updating a record that already exists with this and check if it does update.
var sn_rec = new GlideRecord(MY_TABLE); sn_rec.addQuery('source_id', '=', json['source_id']); sn_rec.setLimit(1); sn_rec.query();
sn_rec.next(); for (var p in json) { sn_rec.setValue(p, json[p]); } sn_rec.update();
you could also try this rather than setValue...
for (var p in json) { sn_rec.p = json[p]; }
If I helped you with your case, please click the Thumb Icon and mark as Correct.