Is there way (public API preferably) to fetch list of CI's related to application service (like "List CIs" link behavior on business service display page)?

Pratiksha7
Kilo Contributor

Business service display page has a link with caption "List CIs". This link when clicked returns related CI list. I want to fetch such list using API or Script. But I couldn't find any public API for the same. I also tried to write script but it returns way more CI's than I am interested in. Is there a away to fetch exact list of CI's returned by "List CIs" link which makes following REST call :
https://devxxxxx.service-now.com/cmdb_ci_list.do?sysparm_query=sys_idINjavascript:saListCis('sys_id')

While "ListCIs" link returns only 11 CIs, my script returns 676 CIs (when break condition is on 100 level).

My scirpt:
var sys_id = g_request.getParameter("systemId");
var record  = new GlideRecord('cmdb_rel_ci');
var childList = [];
var i = 0;
childList.push(sys_id);
while(i < childList.length) {
record = new GlideRecord('cmdb_rel_ci');
record.addQuery('parent', childList[i]);
g_processor.writeOutput("text/plain", "\n\nCalling for " + i + "\t value : " + childList[i] + "\t length : " + childList.length);
record.query();
record.getAttribute();
var relId = "";
while (record.next()) {
    relId = record.getValue("sys_id");
    childList.push(record.child.toString());
}
if(relId == "")
    g_processor.writeOutput("text/plain", "\tDetails not found for: " + childList[i]);
i++;
    if( i > 100)
        break;
}

Do we need to add any other conditions in script or is there any other way to achieve "List Cis" behavior?

1 ACCEPTED SOLUTION

Rich Dennis
Tera Expert

That URL is has the information that you need to build a REST query. "cmdb_ci" is the table name, and "sysparm_query" is an option in REST URIs. In your instance you should navigate to the "REST API Explorer", which provides a GUI to help build a REST URI. It also has code examples for a few different scripting languages such as JavaScript and cURL.

https://developer.servicenow.com/app.do#!/search?category=All&v=london&q=rest%20api%20explorer&page=1 

View solution in original post

5 REPLIES 5

Rich Dennis
Tera Expert

If I understand correctly...

On a Business Service record there will be a Related List with the CIs that make up that BS. If you right-click on the bread-crumb trail you'll get an option to open in new window. That new window will have the table and filter that you want to use as your basis for the REST call. You can use the REST API Explorer within your instance to test this out.

Hi Rich,

Thanks for the reply.

You are talking about https://devxxxxx.service-now.com/cmdb_ci_list.do?sysparm_query=sys_idINjavascript:saListCis('SYSID')
REST call which gets invoked on "List CIs" link click, right? It is an internal API which returns HTML response. We require JSON / XML response.

 So given a SysId, API should return list of related CIs in form of JSON/XML. Any more suggestions for this?

Rich Dennis
Tera Expert

That URL is has the information that you need to build a REST query. "cmdb_ci" is the table name, and "sysparm_query" is an option in REST URIs. In your instance you should navigate to the "REST API Explorer", which provides a GUI to help build a REST URI. It also has code examples for a few different scripting languages such as JavaScript and cURL.

https://developer.servicenow.com/app.do#!/search?category=All&v=london&q=rest%20api%20explorer&page=1 

Thank you Rich.

Following query worked as you mentioned:
curl -H "Accept:application/json" --user "userName:password" "https://devXXXXX.service-now.com/api/now/table/cmdb_ci?sysparm_query=sys_idINjavascript:saListCis('SYSID')" --request GET --header "Accept:appl;2"