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

Try this:

 

getIncCap(current.number, current.cmdb_ci.ref_cmdb_ci_appl.u_business_capability);

var getIncCap = function (number, buscaps)
{	
	var retString = [];
	// the field is a list field, so process those individually
	// get cmdb_ci_business_process
	//gs.log("Get business capabilty for: " + 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
    while (cbp.next())
    {
        // go to next record if nill
        if(gs.nil(cbp.u_capability_category))
        {
            continue;
        }
		//Now find the capability category
		var bcc = new GlideRecord('u_business_capability_category');
		bcc.addQuery('sys_id', 'IN', cbp.getValue('u_capability_category'));
		bcc.query();
        while (bcc.next())
        {
			//gs.log("Get category associated with business capability: " + number + " has business capability category: " + bcc.u_category_name + ".");
			retString.push(bcc.getValue('sys_id'));
	    }
    }
    gs.log(JSON.stringify(retString));
	return retString;
}

Getting this error: 

Error during JavaScript evaluation com.snc.pa.dc.ScriptException: getIncCap is not a function.TypeError: getIncCap is not a function. (<refname>; line 1) in script: getIncCap(current.number, current.cmdb_ci.ref_cmdb_ci_appl.u_business_capability); 

Also, since I'm referencing the number and business capability, don't I have to choose them in the fields above the script? Can't get to the capability field though since it's on the child of the CMDB_CI field

find_real_file.png

Adam Stout
ServiceNow Employee
ServiceNow Employee

1) You may have noticed I didn't test this.  Put the call at the bottom of the script instead of the first line.

2) You can't (at least aren't supposed to be able to) dot walk in a script, you need to (want to) dot walk in the fields.   All references to current should be explicitly named in the fields.

Adam - thank you for helping me with this -- i REALLY appreciate it. Moving the call to the bottom got rid of the error, the script ran fine but collected to many records for the categories. Hundreds more than there should be. 

For example - the category for Claims Management show 599 records, but the score is showing 1797. I would expect the number of records and the score to match. 

I will spend some time later this afternoon to continue debugging this and let you know how it goes. Thanks again for your help.