I want to print 10 latest incidents in Scripts background

NaveenKumarSN
Tera Expert

Hi Everyone,

 

If run the below script 

 

var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.orderByDesc('sys_created_at');
inc.setLimit(10);
inc.query();
while(inc.next()){
gs.print(inc.number);
}

 

Getting below out put 

[0:00:00.031] Script completed in scope global: script


Script execution history and recovery available here


*** Script: INC0008001
*** Script: INC0010078
*** Script: INC0010074
*** Script: INC0000015
*** Script: INC0000016
*** Script: INC0000017
*** Script: INC0000018
*** Script: INC0000019
*** Script: INC0000020
*** Script: INC0000025

but how would i these latest or not

Please clarify

Please share me the correct script to print latest 10 incidents
4 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@NaveenKumarSN 

Small correction. it should be sys_created_on and not sys_created_at

you are using orderByDesc() so that guarantees latest records are picked based on sys_created_on

you can verify this on incident list by sort z to a on the Created Date field

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@NaveenKumarSN Please update your script as follows.

 

var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.orderByDesc('sys_created_on');
inc.setLimit(10);
inc.query();
while(inc.next()){
gs.print(inc.number);
}

To verify if the output is correct or not, you need to go to incident list and sort it by created on in descending order, match the first 10 incident numbers are matching with you numbers printed in the script. If it matches than the script is working fine.

View solution in original post

Deepak Shaerma
Kilo Sage

Hi @NaveenKumarSN 

Order the results by the sys_created_on or sys_updated_on field in descending order, and then limit the result set to 10.

 

var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on'); // Order by creation date in descending order (latest first)
gr.setLimit(10); 
gr.query(); 

var count = 0;
while (gr.next()) {
    count++;
    gs.print("Incident #" + count + ":");
    gs.print("Number: " + gr.number);
    gs.print("Created On: " + gr.sys_created_on.getDisplayValue());
    gs.print(""); // Print a blank line for better readability
}

 

 
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 

View solution in original post

Getting undefined for 10 times

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@NaveenKumarSN 

Small correction. it should be sys_created_on and not sys_created_at

you are using orderByDesc() so that guarantees latest records are picked based on sys_created_on

you can verify this on incident list by sort z to a on the Created Date field

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Sandeep Rajput
Tera Patron
Tera Patron

@NaveenKumarSN Please update your script as follows.

 

var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.orderByDesc('sys_created_on');
inc.setLimit(10);
inc.query();
while(inc.next()){
gs.print(inc.number);
}

To verify if the output is correct or not, you need to go to incident list and sort it by created on in descending order, match the first 10 incident numbers are matching with you numbers printed in the script. If it matches than the script is working fine.

Getting undefined for 10 times

Deepak Shaerma
Kilo Sage

Hi @NaveenKumarSN 

Order the results by the sys_created_on or sys_updated_on field in descending order, and then limit the result set to 10.

 

var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on'); // Order by creation date in descending order (latest first)
gr.setLimit(10); 
gr.query(); 

var count = 0;
while (gr.next()) {
    count++;
    gs.print("Incident #" + count + ":");
    gs.print("Number: " + gr.number);
    gs.print("Created On: " + gr.sys_created_on.getDisplayValue());
    gs.print(""); // Print a blank line for better readability
}

 

 
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma