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 Huge records.. Best Practice

kailashthiyagar
Kilo Guru

Is there a way to do bulk update other than this approach. This script works fine but i feel it might be causing performance issue.. Its one time update only.. But still would like to know what are the other alternative best practices, we have

var macRec=new GlideRecord('x_opt_macro_govern_macros');

  macRec.addQuery('number','!=','');

  macRec.query();

  while(macRec.next()){

  macRec.active='true';

  macRec.update();

  }

1 ACCEPTED SOLUTION
6 REPLIES 6

Just wanted to mention a word of warning for anyone reading this thread. When using updateMultiple(), it is critical to use the setValue() method and not to set field values directly. Per the documentation:



"When changing field values, use setValue() instead of directly setting the field (field = something). When using updateMultiple(), directly setting the field (gr. state = 4) results in all records in the table being updated instead of just the records returned by the query."


http://wiki.servicenow.com/index.php?title=Scoped_GlideRecord_API_Reference#updateMultiple.28.29



NOTE: Be very careful when using updateMultiple. You might unexpectedly update every record in your system!!


sachin_namjoshi
Kilo Patron
Kilo Patron

Yes Kailash,



You can modify your code like below to use updatemultiple() method to improve performance.



  1. var macRec=new GlideRecord('x_opt_macro_govern_macros');  
  2.   macRec.addQuery('number','!=','');  
  3.   macRec.query();  
  4.   macRec.setValue('active',true);
  5.   macRec.updateMultiple();


Regards,


Sachin