Slow api response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 01:14 AM
Hi Folks,
Below GlideRecord "usrGr" in code returns records <=10000 it is working fine but it is slow in execution. It is taking approx 9-10 mins to prepare response payload in postman.
Is there any way to optimize this code to avoid such delay in response ?
I want to send all table fields and values in response to 3rd party system.
Any help appreciated!!
TIA
while (usrGr.next()) {
var obj = {};
for (var prop in usrGr) {
if (JSUtil.notNil(usrGr[prop].getJournalEntry(1))) {
obj[prop] = usrGr[prop].getJournalEntry(-1);
} else {
obj[prop] = usrGr.getDisplayValue(prop);
}
}
arr.push(obj);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 01:33 AM
you should optimize the query then only performance can be improved.
Can you share complete script?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 01:54 AM - edited 09-18-2023 02:02 AM
Hey @Ankur Bawiskar ,
Thanks you for quick response.
Query is simple
stateIN1,4,2^sys_created_onONLast 3 months@javascript:gs.beginningOfLast3Months()@javascript:gs.endOfLast3Months()
Below comm questions also says for and if condition in while loop cause slowness
https://www.servicenow.com/community/developer-forum/scripted-rest-api-running-slow/m-p/1798901
var usrGr = new GlideRecord("incident");
usrGr.addEncodedQuery("stateIN1,4,2^sys_created_onONLast 3 months@javascript:gs.beginningOfLast3Months()@javascript:gs.endOfLast3Months()");
usrGr.orderByDesc("sys_created_on");
usrGr.query();
var arr = [];
while (usrGr.next()) {
var obj = {};
for (var prop in usrGr) {
// if (JSUtil.notNil(usrGr[prop].getJournalEntry(1))) {
// obj[prop] = usrGr[prop].getJournalEntry(-1);
// } else {
obj[prop] = usrGr.getDisplayValue(prop);
// }
}
arr.push(obj);
}
If I comment above 4 lines the code is really fast. Plz suggest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 02:07 AM
you are checking for every field of that table.
what's your business requirement? what are you doing in those 4 lines basically?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 02:09 AM
I want to send all table fields and values with work_notes in response to 3rd party system.