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

bernyalvarado
Mega Sage

Hi Vikrant,



A couple of pointers that may be helpful:



a) make sure you have a declaration for your GlideRecord object:


var assignmentObj = new GlideRecord('yourDesiredTable');



b) Only use .initialize() when you're in the process of creating and inserting a new record. When this is the case, you don't need to do a .query().



c) Using the GlideRecord for multiple queries appears like a dangerous practice. I'll rather suggest you either use various GlideRecords (if you're in the server side invoking these) or employ a script include and methods that will independently retrieve the set of records or results that you need on each step of your operation.



I hope this helps!



Thanks,


Berny


sharmavikrant
Kilo Expert

Thanks for your reponse Berny,



Appreciate your suggestion.


I have done a)


for b), I would have to use .initialize() cause a) is global so if I don't so it would be .addEncodedQuery(queryString) & .addEncodedQuery(queryString1). Correct me if I am wrong.



c) doesn't fit with the requirement, as I am doing an enhancement it doesn't seemed to me as a good idea.



I was basically after, of how the GlideRecord behaves and how it should behave in this scenario. If you can share your view on that, it would be appreciated


Thanks for your resonse again.


Hi Vikrant,



You're welcome. If you're just trying to understand the code that you posted. Here's how you will read it:



var queryString ;



//Block 1: Queries whichever table was associated to the GlideRecord assignmentObj using the queryString.


// It appears wrong that the query string is never assigned to anything.


// The query most probably would then retrieve all the records



assignmentObj.initialize();


assignmentObj.addEncodedQuery(queryString);


assignmentObj.query();



logic1 = assignmentObj.getRowCount();





// Block 2: If at least there was one record on the table, then lets try to do another query against


// the same table but this time using the queryString1.


// It also appears wrong that the queryString1 is not set to anything.


// The result would then be exactly the same as the one on the block1



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


  var queryString1;


  assignmentObj.initialize();


  assignmentObj.addEncodedQuery(queryString1);


  assignmentObj.query();


  logic2 = assignmentObj.getRowCount();


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


}



// Block 3: Keep in mind you did an initialize and then query for the results within the scope of Block 2.


// If you're not seen the "Execute the loop" it could be because the query was done within the scope of the if


// and now you're trying to loop through it outside of the scope of the if.


// That will be the only explanation i could think of why you will see a result when adding .query() before it and


// not when keeping the .query within the if block in Block 2.  



if(assignmentObj.next()){


gs.log("Execute the loop");


}


If you have to write an enhancement on this code, I would still suggest to rewrite it. It's a bit messy. >P



Doing so will save you from headaches on the future.



Thanks,


Berny