servicesAffectedByCI - List of CIs between CI and services?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2023 12:25 PM
I'm using CIUtils - servicesAffectedByCI(String CI_sys_id) documented here:
Is it possible to enable debug or something else to produce a list of the CIs between the CI specified and the impacted services returned?
Here is a simple illustration of a service with dependencies:
ServiceABC
CI3
CI2
CI1
CI0
Using servicesAffectedByCI with the sys_id of CI0, it will return the sys_id of ServiceABC. But I'd like to know CI1, CI2, and CI3 are in the path.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2023 12:15 AM
Hi @Ronald Lucas TA ,
Unfortunately, there is no built-in debugging or logging feature for the servicesAffectedByCI function in ServiceNow.
However, you can create a custom script to traverse the CI relationships and get a list of CIs between the specified CI and the impacted service.
Here is an example of how you can achieve this:
var ciUtils = new CIUtils();
var ciSysId = '<your CI sys_id here>';
var serviceSysId = ciUtils.servicesAffectedByCI(ciSysId);
var ciPath = [];
// Recursively traverse the CI relationships to build a list of CIs in the path to the impacted service
function traverseCiRelations(sysId) {
var ciGr = new GlideRecord('cmdb_rel_ci');
ciGr.addQuery('child', sysId);
ciGr.query();
while (ciGr.next()) {
var parentSysId = ciGr.getValue('parent');
ciPath.push(parentSysId);
traverseCiRelations(parentSysId);
}
}
// Traverse the CI relationships starting from the specified CI sys_id
traverseCiRelations(ciSysId);
// Output the list of CIs in the path to the impacted service
gs.info('CI path to impacted service (' + serviceSysId + '): ' + ciPath.join(', '));
You can run this script in a background script or from the browser console. It will recursively traverse the CI relationships starting from the specified CI and build a list of CIs in the path to the impacted service.
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2023 06:07 AM
Hi @Ratnakar7.
I appreciate you taking the time to share this. I tried it on my PDI with a small set of data, and it works. But in a real environment, it finishes with many lines of errors starting with:
Couldn't decipher the stack trace resulting from the following JavaScriptException:
java.lang.StackOverflowError: org.mozilla.javascript.JavaScriptException: java.lang.StackOverflowError: org.mozilla.javascript.Context.makeJavaScriptException(Context.java:2080)
Most of the errors repeat over and over again. I may try to figure this out, but if you have a suggestion, please let me know.
Thanks,
Ron