The CreatorCon Call for Content is officially open! Get started here.

Pring Lat 5 records of current logged in user

Chiranjeevi K
Tera Expert

I am sure the below script will print the last 5 records created on the system but am looking to print last 5 records assigned to the logged in user , could anyone please help me with the change on the below script.

var gr = new GlideRecord('incident);
gr.orderByDesc('sys_created_on');
gr.SetLimit(5);
gr.query();
while(gr.next(){
gs.print('incident number: '+gr.number);
}

11 REPLIES 11

Chiranjeevi K
Tera Expert

Anurag Tripathi  Could you please give some idea on to this 
I tried below script but still not working as expected 

var userId = gs.getUserID();
var gr = new GlideRecord('incident');
//gr.addQuery('user',userId);
gr.orderBy('sys_created_on');
gr.setLimit(10);
gr.query();
while(gr.next()){
    gs.print('Incident Number:'+gr.number);
}

 

 

Kilo Patron

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

You would need at least to fix the below, as that will never work:

gr.SetLimit(5);

 

Its setLimit, not SetLimit.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Amit Verma
Kilo Patron
Kilo Patron

Hi @Chiranjeevi K 

 

Please use below code and test it using background scripts. Probably, setLimit() would be the culprit as suggested by @Mark Roethof .

 

var currentUser = gs.getUserID();
var gr = new GlideRecord('incident');
gr.addQuery('caller_id='+currentUser);
gr.orderByDesc('sys_created_on');
gr.setLimit(5);
gr.query();
while(gr.next()){
gs.print('incident number: '+gr.number);
}

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

abhishekdalvi
Tera Guru

Hi @Chiranjeevi K ,

Please check with below script.

var loggedInUser = gs.getUserID();
var gr = new GlideRecord("incident");
gr.addEncodedQuery('assigned_to='+loggedInUser);
gr.orderByDesc('sys_created_on');
gr.setLimit(5);
gr.query();
while(gr.next()){
gs.print('incident number: '+gr.number);
}
 
Please mark this response as correct or helpful if it assisted you with your question.
 
Regards,
Abhishek Dalvi
Regards,
Abhishek Dalvi

Rajdeep Ganguly
Mega Guru

Sure, you can modify the script to filter the incidents assigned to the current logged in user. Here is the updated script: javascript var gr = new GlideRecord('incident'); gr.addQuery('assigned_to', gs.getUserID()); // filter incidents assigned to the current user gr.orderByDesc('sys_created_on'); gr.setLimit(5); gr.query(); while(gr.next()) { gs.print('incident number: ' + gr.number); } Here are the changes summarized: - Added a new line gr.addQuery('assigned_to', gs.getUserID()); to filter incidents assigned to the current user. - gs.getUserID() is used to get the sys_id of the current logged in user. - This line should be added before gr.orderByDesc('sys_created_on'); to ensure the filter is applied before sorting and limiting the results.