Need help with PA script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2018 05:51 AM
I'm trying to build a script to use a breakdown. I've reached out to the community and SN before regarding this but it still isn't working as expected.
The breakdown should be coming from a glide list's related field. The structure of the data goes cmdb_ci > cmdb_ci_appl > u_business_capability (glide list to the cmdb_ci_business_process table)> u_capability_catagory
Applications can use multiple capabilities associated with a category and that's where my problem lies. The records are being counted multiple times if there are more than one capability per category. I only want the record to count once against a category. I need to somehow remove these duplicates from the script. SN helped build this script and I need further help as I am not the greatest coder. Please help.
getIncCapCategory(current.cmdb_ci);
function getIncCapCategory(cmdbRecID) {
var retString = [];
// check for empty cmdb_ci field
if (cmdbRecID == null) {
return retString;
}
// get cmdb_ci_app record
// gs.log("getIncCapCategory: looking in cmdb_ci_appl for " + cmdbRecID + ".");
var capp = new GlideRecord('cmdb_ci_appl');
capp.addQuery('sys_id', cmdbRecID);
capp.query();
if (capp.next()) {
// check for values
var buscap= capp.u_business_capability;
if (buscap == '')
return retString;
// the field is a list field, so process those individually
var buscaps = buscap.split(",");
var pCapCat = ""; // pervious capability category, for not processing duplicates
for (var i = 0; i < buscaps.length; i++) {
// get cmdb_ci_business_process
// gs.log("getIncCapCategory: " + current.number + " looking in cmdb_ci_business_process for " + buscaps[i] + ".");
var cbp = new GlideRecord('cmdb_ci_business_process');
cbp.addQuery('sys_id', buscaps[i]);
cbp.query();
if (cbp.next() && (cbp.u_capability_category != null) && (pCapCat != cbp.u_capability_category.toString())) {
// gs.log("pCapCat: " + pCapCat + ", cbp.u_capability_category: " + cbp.u_capability_category.toString() + ".");
// gs.log("looking in u_business_capability_category for " + cbp.u_capability_category + ".");
// Now find the capbility category
var bcc = new GlideRecord('u_business_capability_category');
bcc.addQuery('sys_id', cbp.u_capability_category);
bcc.query();
if (bcc.next() && (pCapCat != bcc.u_catagory_name.toString())) {
//gs.log("getIncCapCategory: " + current.number + " has business capability category: " + bcc.u_category_name + ".");
retString.push(bcc.sys_id);
}
pCapCat = cbp.u_capability_category.toString();
}
}
}
return retString;
}
- Labels:
-
Dashboard
-
Performance Analytics
-
Reporting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2018 08:36 PM
Still seems overly complicated (you have a list with multiple that can additionally have multiple values?).
But I think the problem you are having now just an array pointer.
Change this line:
retString.push(bcc.sys_id);
to this:
retString.push(bcc.getValue('sys_id'));
This will ensure that the string for sys_id gets pushed to the array instead of a pointer to it (which gets overwritten with every loop). The result of not doing this is you get only the last value in the array. This change will make sure you get all the values.
You had it before, but never be afraid to put in a gs.info(JSON.stringify(retString)) to debug what you are actually getting. Sometimes you just have to see what is really going on.
Let me know how it goes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2018 11:29 AM
Hi Adam,
I've changed that line but it still seems that I'm getting the last value. I uncommented my logs so that I can see what Incidents are getting what categories and it's logged multiple times but always with the same name. Also added the stringefy log and I'm getting one sys ID logged.
These applications have multiple capabilities associated with them, and those capabilities map to a category. Sometimes there are capabilities listed that map to a few different categories. One capability maps to one category, but since multiples capabilities can be listed, then that's when there are multiple categories. Does that make sense? The structure is a bit confusing.
I changed the "IFs" to "Whiles" to see if that would help and it duplicated everything and gave me a score that was way too high.
I think I'm close, but am hitting a road block somewhere.
getIncCap(current.cmdb_ci);
function getIncCap(){
var retString = [];
//dot walk to the business capability glide list field located on the application table (cmdb_ci_appl)
var buscap= current.cmdb_ci.ref_cmdb_ci_appl.u_business_capability;
//split the list of capabilities at the comma
var buscaps = buscap.split(",");
// the field is a list field, so process those individually
// get cmdb_ci_business_process
gs.log("Get business capabilty for: " + current.number + " looking in cmdb_ci_business_process table for " + buscaps + ".");
var cbp = new GlideRecord('cmdb_ci_business_process');
//Check for capabilities contained on that application record associated with the incident
cbp.addQuery('sys_id', 'IN', buscaps);
cbp.query();
//If the capabilities exist on the CMDB_CI_BUSINESS_PROCESS table, then proceed
if (cbp.next() && (cbp.u_capability_category != null)) {
//Now find the capbility category
var bcc = new GlideRecord('u_business_capability_category');
bcc.addQuery('sys_id', 'IN', cbp.u_capability_category);
bcc.query();
if (bcc.next()) {
gs.log("Get category associated with business capability: " + current.number + " has business capability category: " + bcc.u_category_name + ".");
retString.push(bcc.getValue('sys_id'));
gs.log(JSON.stringify(retString));
}
//pCapCat = cbp.u_capability_category.toString();
}
return retString;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2018 12:29 PM
Can we try like,
if (cbp.next() && (cbp.u_capability_category != null)) {
var bcc = new GlideRecord('u_business_capability_category');
bcc.addQuery('sys_id', 'IN', cbp.u_capability_category);
bcc.query();
if (bcc.next()) {
gs.log("Get category associated with business capability: " + current.number + " has business capability category: " + bcc.u_category_name + ".");
retString.push(bcc.sys_id.toString());
}
}
return 'sys_idIN' + retString;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2018 12:43 PM
For a scripted breakdown, you need to return one of three things:
- sys_id
- array of sys_ids
- comma separated list of sys_ids
Including sys_idIN in the return string will cause issues.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2018 12:47 PM
Sure, Thank you!