Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

GlideRecord update fails

Daniel Gurney1
Tera Expert

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);
}

 

1 ACCEPTED SOLUTION

Daniel Gurney1
Tera Expert

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); 
}

View solution in original post

7 REPLIES 7

Gary Fawcett1
Tera Guru

Do you have an example of what the JSON object looks like?

The JSON is just like the table except without sys_ columns.  For example, if the table has a column "name" the JSON has "name".

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]); } }

Sebas Di Loreto
Kilo Sage

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.