10 Replies · Latest reply on Dec 24, 2017 12:49 PM by Chuck Tomasi

    How to reset GlideRecord object

      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!

        • Re: How to reset GlideRecord object
          Chuck Tomasi

          Add the new GlideRecord inside your for loop.

           

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

               var gr = new GlideRecord('myTable');

               gr.addQuery('column', i)

               gr.query();

               gr.getrowcount();

          }

          1 of 1 people found this helpful

          --Chuck Tomasi
          --Sr. Technical Product Marketing Mgr, Now Platform
          --Join me on the Community live stream (most) weekdays at 8:00AM ET
          --https://youtube.com/user/servicenowcommunity

            • Re: How to reset GlideRecord object
              Vaibhav Bhatnagar

              Hi Chuck,

               

              Thanks for your response!

              In each loop if I create var gr = new GlideRecord('myTable'), will it not be create unnecessary objects?

                • Re: How to reset GlideRecord object
                  Chuck Tomasi

                  No, because we haven't saved or committed anything. There is no call to gr.insert() or gr.update().

                   

                  I also noted that both of us are using getRowCount() improperly (it's not all lower case) and we're not outputting anything. Try this.

                   

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

                       var gr = new GlideRecord('myTable');

                       gr.newRecord();

                       gr.addQuery('column', i)

                       gr.query();

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

                  }

                   

                   

                  --Chuck Tomasi
                  --Sr. Technical Product Marketing Mgr, Now Platform
                  --Join me on the Community live stream (most) weekdays at 8:00AM ET
                  --https://youtube.com/user/servicenowcommunity

                    • Re: How to reset GlideRecord object
                      Vaibhav Bhatnagar

                      In actual logic I am going to update a column in the loop. Is it recommendable to use it?

                        • Re: How to reset GlideRecord object
                          Chuck Tomasi

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

                               }

                          }

                          --Chuck Tomasi
                          --Sr. Technical Product Marketing Mgr, Now Platform
                          --Join me on the Community live stream (most) weekdays at 8:00AM ET
                          --https://youtube.com/user/servicenowcommunity

                  • Re: How to reset GlideRecord object
                    Santosh Sahoo

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

                    }

                    Cheers!

                    Santosh

                      • Re: How to reset GlideRecord object
                        Chuck Tomasi

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

                        }

                        1 of 1 people found this helpful

                        --Chuck Tomasi
                        --Sr. Technical Product Marketing Mgr, Now Platform
                        --Join me on the Community live stream (most) weekdays at 8:00AM ET
                        --https://youtube.com/user/servicenowcommunity

                      • Re: How to reset GlideRecord object
                        Ed Wajs

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

                                  }

                             }

                        }

                        1 of 1 people found this helpful
                          • Re: How to reset GlideRecord object
                            Chuck Tomasi

                            Someone has been hanging around the Technical Best Practices pages! Nice job!

                            --Chuck Tomasi
                            --Sr. Technical Product Marketing Mgr, Now Platform
                            --Join me on the Community live stream (most) weekdays at 8:00AM ET
                            --https://youtube.com/user/servicenowcommunity

                            • Re: How to reset GlideRecord object
                              Chuck Tomasi

                              One minor change to the script above, change the for loop int, to var (see bold text below.)

                               

                              updateMyRecords();

                               

                              function updateMyRecords() {

                                   for (var 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();

                                        }

                                   }

                              }

                              --Chuck Tomasi
                              --Sr. Technical Product Marketing Mgr, Now Platform
                              --Join me on the Community live stream (most) weekdays at 8:00AM ET
                              --https://youtube.com/user/servicenowcommunity