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

Allen Andreas
Administrator
Administrator

This has a lot of replies by a lot of different people on this. Wow...

As far as trying to be slick with some of the JS going on, that some seem to defend and some are against (as far as best practice)...I really think this should be stripped back down to basics and just ensure that the thing is working as intended even from a very base level (like returning ANYTHING...). It's cool and all to use shortcuts and all that, but when things get wonky, it can get messy.

I would start with commenting out most of this and just slowly getting back something, then increasing your code from there.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

ktjstn
Kilo Expert

I think this one is really not returning values. I'll just need business rule instead. Thank you for all your help guys! Really appreciate it 🙂

Alright. If you need any further assistance, please let us know.

Also, please mark any other posts through this entire thread as Helpful, if they were.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!