Why is my custom table not working properly with .next() in GlideRecord?

rmiesen
Kilo Expert

I have created a custom table and am attempting to use it on an Inbound Email Action.   I have run into a problem however in using GlideRecord to query the table.   What is happening is that I will issue a Query and get the expected number of rows back as seen by .getRowCount().   I can issue a .next() to the query and access the first record returned without issue.   However from this point on it does not believe there are any more records.   If I run .hasNext() it returns false and if I try to use .nex() nothing changes.

 

This is my first foray into custom tables so I'm not sure if I missed some specific field needed to allow queries to properly work.   The table does not extend anything and has one reference.   Other than that it has the default values, 2 choice lists and the rest are strings.   If I change the sorting parameters I can alter which record is first without issue and using the same code I can put in an existing table and navigate the records without issue.   The table is extensible right now and autonumbered.

 

Any help would be greatly appreciated.   Here is the code I'm using, ignore that it is using a count instead of having .next() in the while loop, it is setup this way from debugging this issue.

 

var lookup = new GlideRecord('u_failed_event_rules');
lookup.query();
var count = lookup.getRowCount();
logger.logInfo('row count ' + count)
while (i < count){
         logger.logInfo('has next ' + lookup.hasNext());
         lookup.next();
         logger.logInfo('query details ' + ' policy ' + lookup.u_policy.toString() + ' client ' + lookup.u_client + ' label ' + lookup.u_schedule_label + ' type ' + lookup.u_schedule_type);
}

In the logging you can see this from the logger statements( note that the 3 query details should all be different):

row count 3

has next true

has next false

has next false

query details policy SQL_NJTOSQLC03_Daily client njtosqlc03 label TEST2 type FULL

query details policy SQL_NJTOSQLC03_Daily client njtosqlc03 label TEST2 type FULL

query details policy SQL_NJTOSQLC03_Daily client njtosqlc03 label TEST2 type FULL


1 ACCEPTED SOLUTION

I was able to solve it, the solution makes absolutely no sense to me but it is working.



All I did was change my variable for the query from 'lookup' to 'gr' and now it works.   No logic changes at all were made.


View solution in original post

16 REPLIES 16

Sorry, accidentally cut off where I initialize i.   It is being set to 0 prior to the loop though.


marcguy
ServiceNow Employee
ServiceNow Employee

the only time I've seen next() have this issue is on sys_template where there is already an out of the field box called 'next', in those cases you have to use _next() but shouldn't be the case on a custom field unless you've extended it from a table with a 'next' field



GlideRecord - ServiceNow Wiki


Ya I even tried _next() just to be sure but since next() works the first attempt and fails after that it was mostly just because I was low on things to try.



I have now tried creating a new table which did not help and then created a table that extends task just to give it a shot and no luck there either.   I can't seem to find why it would return the correct number of rows but only thing one record is available.


marcguy
ServiceNow Employee
ServiceNow Employee

are you doing while (lookup.next()){?



  1. var lookup = new GlideRecord('u_failed_event_rules');  
  2. lookup.query();  
  3. var count = lookup.getRowCount();  
  4. logger.logInfo('row count ' + count)  
  5. while (lookup.next()){
  6.         logger.logInfo('has next ' + lookup.hasNext());           logger.logInfo('query details ' + ' policy ' + lookup.u_policy.toString() + ' client ' + lookup.u_client + ' label ' + lookup.u_schedule_label + ' type ' + lookup.u_schedule_type);  
  7. }  

I was able to solve it, the solution makes absolutely no sense to me but it is working.



All I did was change my variable for the query from 'lookup' to 'gr' and now it works.   No logic changes at all were made.