Using .update() on a GlideRecord reference field

Joey Wan Kenobi
Tera Guru

I'm trying to save a change to database catalogs. For some reason, I cant get the change to "stick" after the script is run.

Here's my code. dbCatalogs is a GlideRecord built on the ci_rel_type table. It has been filtered down to have only database catalogs in the parent field.

 

while (dbCatalogs.next()) {

 

  gs.log("Setting the environment for " + dbCatalogs.parent.name + " to " + dbCatalogs.child.used_for, progName);

  dbCatalogs.parent.u_used_for = dbCatalogs.child.used_for;

 

  //Turn off business rules/update recognition before saving

  dbCatalogs.parent.autoSysFields(false);

  dbCatalogs.parent.setWorkflow(false);

  dbCatalogs.parent.update();

 

  count++;

}

 

Do I have to make a GlideRecord on the database catalog tables, get the record based on the parent and then update that?

Sorry for the format; Im not sure how to paste in code format.

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

You can use the getRefRecord() method to load a GlideRecord of the parent field - GlideElement - ServiceNow Wiki



Your code should end up looking something like:



while (dbCatalogs.next()) {


  gs.log("Setting the environment for " + dbCatalogs.parent.name + " to " + dbCatalogs.child.used_for, progName);


  var parentGr = dbCatalogs.parent.getRefRecord();


  parentGr.u_used_for = dbCatalogs.child.used_for;



  //Turn off business rules/update recognition before saving


  parentGr.autoSysFields(false);


  parentGr.setWorkflow(false);


  parentGr.update();



  count++;


}


View solution in original post

1 REPLY 1

Jim Coyne
Kilo Patron

You can use the getRefRecord() method to load a GlideRecord of the parent field - GlideElement - ServiceNow Wiki



Your code should end up looking something like:



while (dbCatalogs.next()) {


  gs.log("Setting the environment for " + dbCatalogs.parent.name + " to " + dbCatalogs.child.used_for, progName);


  var parentGr = dbCatalogs.parent.getRefRecord();


  parentGr.u_used_for = dbCatalogs.child.used_for;



  //Turn off business rules/update recognition before saving


  parentGr.autoSysFields(false);


  parentGr.setWorkflow(false);


  parentGr.update();



  count++;


}