Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Slow api response

Abdul Azeem
Tera Contributor

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);
                }
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Abdul Azeem  

you should optimize the query then only performance can be improved.

Can you share complete script?

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

Hey @Ankur Bawiskar ,

 

Thanks you for quick response.

Query is simple 

stateIN1,4,2^sys_created_onONLast 3 months@javascript&colon;gs.beginningOfLast3Months()@javascript&colon;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&colon;gs.beginningOfLast3Months()@javascript&colon;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

@Abdul Azeem  

you are checking for every field of that table.

what's your business requirement? what are you doing in those 4 lines basically?

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

 I want to send all table fields and values with work_notes in response to 3rd party system.