Need help with PA script

Blair5
Tera Guru

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;
}
13 REPLIES 13

Adam Stout
ServiceNow Employee
ServiceNow Employee

This seems overly complicated.  I would expect that you would dot walk to the list then do one query with the list being something like sys_id IN current.list.field

Then loop through the results and push them into an array which you will return for the breakdown.

You make is sound so easy. 

So, would I remove the first 2 gliderecord queries and do something like??:

getIncCapCategory(cmdb_ci.ref_cmdb_ci_appl.u_business_capability);

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();

 

Adam Stout
ServiceNow Employee
ServiceNow Employee

I'm expecting to see something like this: 

cbp.addQuery('sys_id', 'IN', buscaps);

With no loop.  There should only be one loop which goes through the results.

Hi Adam,

 

Thank you for the help - seems like it's pretty close. If a CI on an incident has a capability for 2 different capability categories, I need it to be counted for each of those. The data looks like its only counting it for one. I failed to mention that they can be associated with more than one category. See my script below:

 

getIncCap(current.cmdb_ci);

function getIncCap(){
	
	//var capp = current.cmdb_ci.ref_cmdb_ci_appl.u_business_capability;
	var retString = [];
	var buscap= current.cmdb_ci.ref_cmdb_ci_appl.u_business_capability;
	var buscaps = buscap.split(",");
	
	// the field is a list field, so process those individually
	// get cmdb_ci_business_process
	var cbp = new GlideRecord('cmdb_ci_business_process');
	cbp.addQuery('sys_id', 'IN', buscaps);
	cbp.query();
	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', cbp.u_capability_category);
		bcc.query();
		if (bcc.next()) {
			retString.push(bcc.sys_id);
		}
		//pCapCat = cbp.u_capability_category.toString();
	}
		return retString;
}