How to fetch the last 10 incident records through background script

vinod96
Tera Contributor

How to fetch the last 10 incident records through background script and middle records also. could you please help any one

3 REPLIES 3

Astik Thombare
Tera Sage

Hi @vinod96 ,

 

Please use below script -

 

var inc = new GlideRecord('incident');

inc.orderByDesc('sys_created_on');

inc.setLimit(10);

inc.addQuery('number','!=','');

inc.query();

while(inc.next()){

gs.info(inc.number);

}

 

This will allow you to fetch last 10 incident records 

 

Please mark  Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on the impact.

 

Regards,

Astik Thombare

Sandeep Rajput
Tera Patron
Tera Patron

@vinod96 Here is the script for you.

 

 

var incidentGR = new GlideRecord('incident');
incidentGR.orderByDesc('sys_created_on');//to get latest incident by created date
incidentGR.setLimit(10);//Set the limit here
incidentGR.query();
while(incidentGR.next());
{
    gs.info('Incident Number: '+ incidentGR.getValue('number'));
}

 

Hope this helps.

SN_Learn
Kilo Patron
Kilo Patron

Hi @vinod96 ,

 

Please find the below background script for getting the last 10 records:

 

var incRec = new GlideRecord('incident');
//'orderByDesc' - order the results descending by 'Created' date
incRec.orderByDesc('sys_created_on');
//'setLimit' is used to limit the number of results returned
incRec.setLimit(10);
incRec.query();
while(incRec.next()){
gs.info(" Incident Record: " + incRec.number);
}

 

 

Output:

 

*** Script:  Incident Record: INC0000601
*** Script:  Incident Record: INC0000055
*** Script:  Incident Record: INC0000047
*** Script:  Incident Record: INC0000053
*** Script:  Incident Record: INC0000052
*** Script:  Incident Record: INC0000051
*** Script:  Incident Record: INC0000050
*** Script:  Incident Record: INC0000049
*** Script:  Incident Record: INC0000046
*** Script:  Incident Record: INC0000038

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.