Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Sort by based on time

Community Alums
Not applicable

Hi all,

 

I need to sort queried record based on time in update filed instated of date .

i have tried the below code it is sorting based on date.

var prob = new GlideRecord('problem');
        query = "sys_updatedBETWEENjavascript:gs.dateGenerate('" + startDate.getLocalDate() + "','00:00:00')@javascript:gs.dateGenerate('" + endDate.getLocalDate() + "','23:59:59')";

 

        prob.addEncodedQuery(query);
        prob.orderBy('sys_updated');
        prob.query();

 

Is there any way to do this

 

Can anyone help me on this

Thanks

6 REPLIES 6

Prasad Dhumal
Mega Sage

Hello Pavithra,

prob.orderBy('sys_updated'); is not correct, it should be sys_updated_on

Further more, prob.orderBy('sys_updated_on.time'); will sort it using the time however as I am not entirely sure about the use case, kindly check the data once sorted.

 

Sohail Khilji
Kilo Patron

@Community Alums 

 

This can help !

 

make sure to update the colon ':'

 

 

var prob = new GlideRecord('problem');
var query = "sys_updatedBETWEENjavascript:gs.dateGenerate('" + startDate.getLocalDate() + "','00:00:00')@javascript:gs.dateGenerate('" + endDate.getLocalDate() + "','23:59:59')";
prob.addEncodedQuery(query);
prob.orderBy('sys_updated_time');  // Sort by the time portion of the sys_updated field
prob.query();

 

 

In the code above, I added the orderBy function to sort the records based on the time portion of the sys_updated field. To achieve this, you can use a field named sys_updated_time (assuming it exists in your table), which represents the time part of the sys_updated field.

 

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Manmohan K
Tera Sage

Hi @Community Alums 

 

var prob = new GlideRecord('problem');
        query = "sys_updatedBETWEENjavascript:gs.dateGenerate('" + startDate.getLocalDate() + "','00:00:00')@javascript:gs.dateGenerate('" + endDate.getLocalDate() + "','23:59:59')";

        prob.addEncodedQuery(query);

        prob.orderBy('sys_updated_on');

        prob.query();
while(prob.next()){

gs.print(prob.number);

}

Community Alums
Not applicable

Hi @Manmohan K ,

 

I given the filed name correctly in my instance but while posting the question i given filed name incorrectly .

my question is I need to sort based on time par only  in  sys_updated_on filed if I use below code it is sorting based on date filed

 

prob.orderBy('sys_updated_on');

 

Thanks