How to reset GlideRecord object

vaibhavbhatnaga
ServiceNow Employee
ServiceNow Employee

Hello,

I have a code like this

var gr = new GlideRecord('myTable');

for(int i=0;i<5;i++){

        gr.addQuery('column', i)

        gr.query();

        gr.getrowcount();

}

Problem is, for i=0 it works but for next iterator it appends the addQuery condition to previous one. Example

i=0 condition is column=0

i=1 condition is column=0^column=1

.

.

i=4 condition is column=0^column=1^column=2^column=3^column=4

My question is, how I can rest the gr after each loop, so that only one addQuery is added not the previous one?

Thanks!

15 REPLIES 15

If you are going to update, then leave off the newRecord() or initialize() methods, absolutely.



for(int i=0;i<5;i++){


        var gr = new GlideRecord('myTable');


        // gr.newRecord(); << -- REMOVED


        gr.addQuery('column', i)


        gr.query();


      gs.info(gr.getRowCount() + ' rows returned from query');


        while (gr.next()) {


                  gr.u_field1 = 'some value'; // replace with your real field and values here


                  gr.update();


        }


}


Hi

I am using the above logic to reset the glide record, but it's not working after first iteration. running the scripts in Scripts-Background

 

var aNumber = 'CCG0001022';
var gr = new GlideRecord('sn_irm_cont_auth_common_control_group');

gr.addQuery('number',aNumber);
gr.query();

if (gr.next()){
var ControlProvider = gr.u_control_provider.authorization_boundary.entity.sys_id;
var ControlProviderName = gr.u_control_provider.authorization_boundary.entity.getDisplayValue();
gs.print("Display control provider is: " + ControlProviderName);
var list = gr.u_control_inhirited.getDisplayValue();
var array = list.toString().split(",");

for (var i=0; i < array.length; i++) {
var ControlName = array[i];
gs.print("Display value is: " + ControlName);
var cr = new GlideRecord('sn_compliance_control');
cr.addQuery('profile.sys_id',ControlProvider);
cr.addQuery('name',ControlName);
cr.query();
if (cr.next()){
gs.print("Display Control Number is: " + cr.getDisplayValue());
gs.info(cr.sys_id);
}


}


}

santoshsahoonis
Kilo Guru

try something like this:



var gr = new GlideRecord('myTable');



for(int i=0;i<5;i++){


        gr.initialize();


        gr.addQuery('column', i)


        gr.query();


        gr.getrowcount();


}


Actually newRecord() is a preferred method over initialize() as it also sets your default values. It's typically used when you are inserting new records, not so much for doing queries, although it may accomplish what you need in lieu of creating the object.



ECMA5 recommends the variables be declared within the block they are used, so perhaps a combination of our solutions...



for(int i=0;i<5;i++){


        var gr = new GlideRecord('myTable');


        gr.newRecord();


        gr.addQuery('column', i)


        gr.query();


        gr.getrowcount();


}


edwajs
ServiceNow Employee
ServiceNow Employee

Vaibhav, you also want to use a more unique variable name than gr, and be sure to encapsulate your code in a function.   I'm going to build on Chuck's earlier code example:




updateMyRecords();



function updateMyRecords() {


        for(int i=0;i<5;i++){


                  var myTableGR = new GlideRecord('myTable');


                  myTableGR.addQuery('column', i)


                  myTableGR.query();


                  gs.info(myTableGR.getRowCount() + ' rows returned from query > '+myTableGR.getEncodedQuery());


                  while (myTableGR.next()) {


                            myTableGR.u_field1 = 'some value'; // replace with your real field and values here


                            myTableGR.update();


                  }


        }


}