Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Get the Newest record out of two on Sys Approval Records

Sunny14
Tera Contributor

Hello Experts, I need to pick the newest approval record [From time stamp] on sysapproval table for a RITM. Please help me out. Thank you in advance. 

 

var approvalRec = new GlideRecord('sysapproval_approver');
approvalRec.addQuery("sysapproval","96f9fcb99772bd5458dfbb4e6253af67"); //Sysid of RITM
approvalRec.addQuery("state","approved");
approvalRec.query();
if(approvalRec.next()){
gs.info(approvalRec.getRowCount());
}

Sunny14_0-1721167584466.png

 

I get below two Records in the result, and I need to pick the Top one [Newest one]. 

Please advise how do I sort to get the Newest one.

Sunny14_1-1721167823728.png

 

 

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Sunny14 ,

 

You can just put the orderByDesc on sys_created_on

Here is the updated script-

var approvalRec = new GlideRecord('sysapproval_approver');
approvalRec.addQuery("sysapproval", "96f9fcb99772bd5458dfbb4e6253af67"); // Sysid of RITM
approvalRec.addQuery("state", "approved");
approvalRec.orderByDesc('sys_created_on'); // Order by created date in descending order
approvalRec.setLimit(1);// this would always return only one record. This is optional if you need it you can use
approvalRec.query();
if (approvalRec.next()) {
    gs.info('Newest Record Created on: ' + approvalRec.sys_created_on);
    gs.info('Approval Record Sys ID: ' + approvalRec.sys_id);
}

The above script will always sort the data with latest record at the top.

Note- User setLimit() - to limit the number of records for returning so that you get only one record.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi @Sunny14 ,

 

You can just put the orderByDesc on sys_created_on

Here is the updated script-

var approvalRec = new GlideRecord('sysapproval_approver');
approvalRec.addQuery("sysapproval", "96f9fcb99772bd5458dfbb4e6253af67"); // Sysid of RITM
approvalRec.addQuery("state", "approved");
approvalRec.orderByDesc('sys_created_on'); // Order by created date in descending order
approvalRec.setLimit(1);// this would always return only one record. This is optional if you need it you can use
approvalRec.query();
if (approvalRec.next()) {
    gs.info('Newest Record Created on: ' + approvalRec.sys_created_on);
    gs.info('Approval Record Sys ID: ' + approvalRec.sys_id);
}

The above script will always sort the data with latest record at the top.

Note- User setLimit() - to limit the number of records for returning so that you get only one record.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!


Thanks & Regards,

Sanjay Kumar

Thank you very much @Community Alums 

Awesome. This is exactly I was looking for. 

Thanks. 

Sunny 

Thank you @Community Alums 

 

This resolves my issue. 

 

Thanks.