Accessing the last record in GlideCollection

RajanMurkute
Mega Guru

Using 'gr.next()' reads the first record in a glide_collection . But if I want to stat reading the records in reverse order (ie. reading the last record, first) how do I do that? Any suggestion will be appreciated. Thanks.

1 ACCEPTED SOLUTION

sachin_namjoshi
Kilo Patron
Kilo Patron
You need to use orderByDesc() like below

var gr = new GlideRecord ('incident');


gr.orderByDesc('sys_created_on');


gr.setLimit(1);


gr.query();


if (gr.next()){


   gs.print (gr.number + ' - ' + gr.sys_created_on);


}

Regards,
sachin

View solution in original post

9 REPLIES 9

RajanMurkute
Mega Guru

Thanks everyone; Sachin_namjoshi, asifnoor and CodingSmith.

Let me redefine my task (this time in details.) I am working on creating a new request form in Service Catalog. On that form, there is a catalog variable 'Request Type'.

When the form is submitted, the workflow will create a corresponding Request Item (RITM). On the RITM, I have put a new catalog  variable, 'Reference Number'. I want to increment this 'Reference Number' each time a new request is sumbmitted.

 

I thought about doing this, by getting a glide_collection of all submitted requests; sort them on 'sys_created_on'; get 'Reference Number' on the last record and generate the next 'Reference Number' by adding one.

Hence, my question is how do I read the last record i glide_collection? 

 

RajanMurkute
Mega Guru

Please ignore my response above. I got the solution. It worked exactly the way everyone suggested. Once again, thanks sachin_namjoshi, asignoor and CodingSmith

Do you mind to mark my answer as correct so that others can benefit from same question?

 

Regards,

Sachin

Kindly mark the answer(s) as helpful and correct if it solves so that the question is removed from the unanswered list and it benefit others.

Great. Kindly mark the comment as helful and correct if it helped you in solving.