How to pass values from script include to client script?

ktjstn
Kilo Expert

I'm trying to get the Risk by using Script Include and Client Script.

Risk was already calculated in the table I just want the exact risk to be retrieved on that table based on the selected recoverabilty, impact, deployment etc.

I also try to put the answer on alert() function but it says the value of answer is null.

Here's my scripts:

1. Script Include

var RiskLookup = Class.create();
RiskLookup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getApplicationRisk: function()
{

	var types = this.getParameter('sysparm_changetype');
	var businessCIs = this.getParameter('sysparm_business');
	var serverCIs = this.getParameter('sysparm_server');
	var impacts = this.getParameter('sysparm_impact');
	var deployments = this.getParameter('sysparm_deploy');
	var recovers = this.getParameter('sysparm_recover');

//	if(types == 'Application'){
	var businessCI = new GlideRecord('cmdb_ci_service');
	businessCI.addQuery('name', businessCIs);
	businessCI.query();
		
	while(businessCI.next())
	{
	  var type = new GlideRecord('u_risk_calculation');
	      type.addQuery('u_change_type', types);
	      type.addQuery('u_business_criticality', businessCI.busines_criticality);
	      type.addQuery('u_impact', impacts);
	      type.addQuery('u_deployment', deployments);
	      type.addQuery('u_recoverability', recovers);
	      type.query();

	while(type.next()){
					
	var appRiskCalc = {};
	appRiskCalc.risk_app = type.u_risk.getDisplayValue() || '';

	var data = new global.JSON().encode(appRiskCalc);
	return data;

	}
    }
 //}
},


	//type: 'RiskLookup'
});​

 

2. Client Script:

function onChange(control, oldValue, newValue, isLoading)
{

	var type = g_form.getValue('u_change_type');
	var businessCI = g_form.getValue('u_business_ci');
	var serverCI = g_form.getValue('u_related_ci');
	var impact_scope = g_form.getValue('impact');
	var deployment = g_form.getValue('u_deployment');
	var recoverability = g_form.getValue('u_recoverability');


	if(type && businessCI && serverCI && impact_scope && deployment && recoverability ){

		var ga = new GlideAjax('RiskLookup');
		ga.addParam('sysparm_name','getApplicationRisk');
		ga.addParam('sysparm_changetype', type);
		ga.addParam('sysparm_business', businessCI);	
		ga.addParam('sysparm_server', serverCI);
		ga.addParam('sysparm_impact', impact_scope);
		ga.addParam('sysparm_deploy', deployment);
		ga.addParam('sysparm_recover', recoverability);

		ga.getXML(getInfo);

	}


	function getInfo(response)
	{   
		var answer = response.responseXML.documentElement.getAttribute("answer");
		answer = JSON.parse(answer);
		alert(answer);
		g_form.setValue('risk',answer.risk_app);

	}

}
22 REPLIES 22

Dawid K1
Tera Contributor
Hi, Kate, BusinessCI.query is in uppercase. Change to businessCI.query() as JS is case sensitive. See if that's the problem.

The answer was still null 😞

David Arbour
Tera Guru

What parameter values are you passing into the AJAX call in your client script? Being that you've named all of your variables as plurals, my first assumption is that you're passing in multiple values. for instance, the value you're passing into businessCIs, is this a single CI name, or is it a comma-separated list of CI names? If it is the latter, your query should look like:

businessCI.addQuery('name', 'IN', businessCIs);

I would be able to give you more assistance with this if you could clarify exactly what values you are passing into the function.

As far as I know you can do addQuery('name' , listOfNames ) and it will automatically resolve to 'IN'. You can easly pass array as an argument and it will work