GlideRecord object not working as expected

sharmavikrant
Kilo Expert

Hi All,

I am have been developing a code where I am required to query for a particular combination of record, check the count of the record, if the count is one only which it checks somewhere down the line with "if(object.next())". Also a code snippet goes like below:

var queryString ;

assignmentObj.initialize();

assignmentObj.addEncodedQuery(queryString);

assignmentObj.query();

logic1 = assignmentObj.getRowCount();

if(logic1 ==0 || logic1 >1) {

  var queryString1;

  assignmentObj.initialize();

  assignmentObj.addEncodedQuery(queryString1);

  assignmentObj.query();

  logic2 = assignmentObj.getRowCount();

  gs.log("2nd Logic: "+logic2);

}

if(assignmentObj.next()){

gs.log("Execute the loop");

}

Now when I execute this, I don't get the last gs.log statement to execute. But if I do a "object.query()" statement, before it, it executes fine. as below:

assignmentObj.query();

if(assignmentObj.next()){

gs.log("Execute the loop");

}

Is this a normal behaviour, I haven't come accross this before. If anyone has any opinion I would appreciate the same?

1 ACCEPTED SOLUTION

No worries Vikrant.



Here's how these work.



Doing a .query() will just execute the query against the database using the query you have defined within your glideRecord. As a result of the .query you will have the cursor of the GlideRecord results ready to jump to the first record in the list as soon as you do a .next()



If you have already done a .next() once and there's more than one record truly you can do another .next() to navigate the cursor to the following record. Doing another .query() most probably will position the cursor right at the beginning of your results (.next() will then move it to the first result).



If you already have iterated through all the list, then the .next() will return false and will not retrieve any other record. In other words, if you already ran a loop of: .next() and you want to iterate again through your results then you probably will need to run the .query() again.



Thanks,


Berny


View solution in original post

8 REPLIES 8

sharmavikrant
Kilo Expert

Apologies Berny for making you do so much of effor on explaining the code, by not being clear enough. QueryStings had a valude that I removed.



Basically, I was looking for am I correct by thinking that for example:



I ran block of query as above and got some records.



I ran a loop to handle the records like:


if(assignmentObj.next()){


gs.log("Execute the loop"); // or anything else


}



Now, am I correct if I again do:



if(assignmentObj.next()){


gs.log("Execute the loop");


}



on the same object, without a .query(), It will not return me any record?



If yes why? Shouldn't it be picking up the records based on the last object.query() statement? Or if we handle the records based on object.query() statement, the object is empty and we have to again run object.query() to get something.


To me .query() appears as trigger of a Gun, press is again and it will give you another bullet .


I just need to get my head around it.



Around making the things tidy, I hope I had the ability to make the Business Analyst (My organisations BAs only ) understand this, as they generally need some solution then the best solutions.


No worries Vikrant.



Here's how these work.



Doing a .query() will just execute the query against the database using the query you have defined within your glideRecord. As a result of the .query you will have the cursor of the GlideRecord results ready to jump to the first record in the list as soon as you do a .next()



If you have already done a .next() once and there's more than one record truly you can do another .next() to navigate the cursor to the following record. Doing another .query() most probably will position the cursor right at the beginning of your results (.next() will then move it to the first result).



If you already have iterated through all the list, then the .next() will return false and will not retrieve any other record. In other words, if you already ran a loop of: .next() and you want to iterate again through your results then you probably will need to run the .query() again.



Thanks,


Berny


Really Appreciate your, descriptive response. Thanks Berny.


You're welcome Vikrant!