orderBy not working for GlideRecord attempting to order on a string field that is calculated from another field

bcronrath
Kilo Guru

Hello all,

I am running into a hard to explain issue where a string field I have is not sorting correctly.   I am testing with just 2 different types of string values possible, and they are just randomly being displayed. The string values are filled via a calculated value which just pulls in the name field from a different field.   Here is my simple testing script:

var glAggr = new GlideRecord('u_monitoring');

glAggr.addQuery('sys_created_on','>',gs.daysAgo(7));

glAggr.orderBy('u_service_name');

glAggr.query();

while(glAggr.next()){

  gs.print(glAggr.u_service_name);

}

There are only 2 possible string values for u_service_name, yet they are randomly being displayed:

*** Script: B Service

*** Script: B Service

*** Script: B Service

*** Script: A Service

*** Script: B Service

*** Script: B Service

*** Script: B Service

*** Script: B Service

*** Script: A Service

*** Script: B Service

*** Script: A Service

*** Script: A Service

*** Script: A Service

*** Script: A Service

*** Script: B Service

*** Script: B Service

*** Script: A Service

*** Script: B Service

*** Script: B Service

*** Script: B Service

*** Script: B Service

*** Script: B Service

*** Script: A Service

*** Script: A Service

*** Script: A Service

*** Script: A Service

*** Script: B Service

Not sure how to explain this behavior.   Anyone ran into something similar before?

Best regards,

Brian

1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage

I may be wrong on this, but I believe this is the issue...



Calculated fields are calculated after the records are retrieved from the database, but sort order is applied during the DB query, so the calculated field won't be calculated yet, hence the order wouldn't apply.



You may have to apply your own sorting on that field after the GlideRecord results are in...


View solution in original post

1 REPLY 1

Jon Barnes
Kilo Sage

I may be wrong on this, but I believe this is the issue...



Calculated fields are calculated after the records are retrieved from the database, but sort order is applied during the DB query, so the calculated field won't be calculated yet, hence the order wouldn't apply.



You may have to apply your own sorting on that field after the GlideRecord results are in...