When searching for records and getting multiple search results

user_ms
Tera Expert

If there are multiple "gr.dates" of the written script, I would like to return the one with the latest 'sys_updated_on' of "gr.date".
Please tell me how to extract it.

 

var gr = new GlideRecord('test_table');
gr.addQuery('test,'00');
gr.query();

while (gr.next()) {
 return gr.date;
}
3 ACCEPTED SOLUTIONS

Community Alums
Not applicable

Hi @user_ms 

Try this script -

var gr = new GlideRecord('test_table');
gr.addQuery('test', '00');
gr.orderByDesc('sys_updated_on'); // Order the results by sys_updated_on in descending order
gr.query();

if (gr.next()) {
return gr.date; // If there's at least one record found
} else {
return null; // Handle case where no records were found
}

Please Mark this Helpful and Accepted Solution, if this helps to resolve your query. 


View solution in original post

Dhananjay Pawar
Kilo Sage

Hi,

Here is the updated code.

 

var gr = new GlideRecord('test_table');
gr.addQuery('test,'00');
gr.orderByDesc('sys_updated_on');
gr.setLimit(1);
gr.query();
while (gr.next()) {
 return gr.date;
}
 
Thanks.

View solution in original post

Abdul Fathah
Mega Guru

Hello,

 

Use orderByDesc attribute to get the latest one.

 

var gr = new GlideRecord('test_table');
gr.addQuery('test,'00');
gr.orderByDesc('sys_updated_on');
gr.query();

if(gr.next()) {
 return gr.date;
}

 

 

Thanks,
Abdul Fathah
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi @user_ms 

Try this script -

var gr = new GlideRecord('test_table');
gr.addQuery('test', '00');
gr.orderByDesc('sys_updated_on'); // Order the results by sys_updated_on in descending order
gr.query();

if (gr.next()) {
return gr.date; // If there's at least one record found
} else {
return null; // Handle case where no records were found
}

Please Mark this Helpful and Accepted Solution, if this helps to resolve your query. 


Dhananjay Pawar
Kilo Sage

Hi,

Here is the updated code.

 

var gr = new GlideRecord('test_table');
gr.addQuery('test,'00');
gr.orderByDesc('sys_updated_on');
gr.setLimit(1);
gr.query();
while (gr.next()) {
 return gr.date;
}
 
Thanks.

Abdul Fathah
Mega Guru

Hello,

 

Use orderByDesc attribute to get the latest one.

 

var gr = new GlideRecord('test_table');
gr.addQuery('test,'00');
gr.orderByDesc('sys_updated_on');
gr.query();

if(gr.next()) {
 return gr.date;
}

 

 

Thanks,
Abdul Fathah
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.