Help avoiding large number of API calls

kmanmx
Mega Contributor

Hi,

 

We have a form that get's submitted as a RITM and has about a dozen or two variables attached to it, using the Snow API i'm finding I have a to do dozens of API calls just to retrieve the variable values each time there is a new form submitted - and figured i'm probably missing something and there is a better way to do it..

 

I grab recently submitted request items at https://848dev.service-now.com/api/now/table/sc_req_item?

 

 

Then looping through each RITM received (often two or three), I get each item_option_mtom by using the req item SysID I got above, and filter just for sc_item_option field e.g. https://848dev.service-now.com/api/now/table/sc_item_option_mtom? sysparm_query=request_item=[Reques...

 

This is where it starts to fall apart, as I seem to have to do an API call for every sc_Item_option returned above, and potentially another one if the variable value is derived from another table (e.g. choose employee from table of all employees) rather than just a string input field. The response to the above API call to sc_item_option_mtom looks like this, I get about 20 sc_item_options returned and have to loop through them all:

 

{
      "sc_item_option": {
        "link": "https://dev.nowplatform.co/api/now/table/sc_item_option/6446913097339150ec1370771153af5f",
        "value": "6446913097339150ec1370771153af5f"
      }
    },
    {
      "sc_item_option": {
        "link": "https://dev.nowplatform.co/api/now/table/sc_item_option/ac46993097339150ec1370771153af98",
        "value": "ac46993097339150ec1370771153af98"
      }
    },

Do I have any other choice than to do API calls against every one of these to get their actual values ? I have tried sysparm_display_value=true, but it still just returns a sys_id as the display value. 

 

Once I have queried all of these one at a time via the sc_item_option link value, I can use sysparm_display_value=true to get value and field name in one API call for each one, but it is still 20 to 30+ requests in total. Even then, as previously mentioned, two or three of these return a sys_id as a value (even with display_value=true), and I have to go off to some other tables for their values. This seems to be the case for variable fields that let you search a table e.g. for a employee name, cost centre etc. 

 

Any help appreciated, new to ServiceNow and REST API's so hoping I am just doing something wrong 🙂

 

4 REPLIES 4

prashanth2
Kilo Guru

Hey, if you want to reduce API calls, you can maybe think of creating scripted REST API, and within that you can write the GlideScript  to query RITM's and then related variables and their values.

Finally create a JSON with all the required data of all the RITMs and send back as response.

By doing this it'd only be a single REST call, and the collection of required data will be done using script.

 

More info on Scripted REST APIs - https://docs.servicenow.com/en-US/bundle/tokyo-application-development/page/integrate/custom-web-ser...

 

If this helps, please mark the answer correct/helpful. 

Hi, thanks for that - it looks like something that would definitely help, so I will figure out how to use it.

jaheerhattiwale
Mega Sage
Mega Sage

@kmanmx Combine the requests and make one API call instead of multiple to bring data in bulk.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Hey, I didn't realise this was possible, have since formatted one big API call using /api/sys_id=1234^ORsys_id=12345^ORsys_id=... etc

 

This is working, and i've reduced the number of API calls by over 90%.