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

Hi David,

Nothing has been logged. I guess there's no value passed

There must be something amiss in your client AJAX call. Are you sure your script include is set to be 'client callable'? If not, check that checkbox, then retry. If so, try adding a console.log(response) statement within your AJAX callback script before you process the response. This will log the value of the XML response to your browser console prior to your code doing any sort of transform on the object. Go to your dev tools in your browser and see what gets logged.

David Arbour
Tera Guru

Looking at your client script, it appears that businessCIs is actually a sys_id value that you're passing in from the client. In your script include, you're querying by name, not by sys_id. try this:

var businessCI = new GlideRecord('cmbd_ci_service');

if (businessCI.get(businessCIs)) {

  // do something

}

Since you're passing in a sys_id, you can use the get() function, which will return only a single GlideRecord object.

I'm still not getting the value desired for Risk field.

DScroggins
Kilo Sage
When passing objects or arrays back to a client script from script include you must stringify them first. Instead of using JSON.encode() try the following: var data = JSON().stringify(appRiskCalc); Hope this helps. --David