Question on script include calling to on submit client script

Venky Kshatriy2
Tera Contributor

 

I have a table in back end , in catalog item in iteam level from country and to county variable are there same way MRVS contain from_country and to_country.based on the this 4 variables i need to auto populate the risk rating

 

in MRVS contain 4 records (ex:i will get the 4 risk rating in that i need to take the highest value 

like high , medium , low, low for this values high is the highest value that value i need to auto populate the risk rating variable value. in script i am getting the one by one value .this values i try to put in array but i getting error

VenkyKshatriy2_0-1721392496383.png

VenkyKshatriy2_1-1721392621998.png

VenkyKshatriy2_2-1721392672085.png

 

above mentioned  diagram i am getting ans in one by one i need to pass the all values in one array

 

 

 

 

script include:

 

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

    getriskratings: function() {

        var getfrom = this.getParameter('sysparm_from');
        var getto = this.getParameter('sysparam_to');
        var getfrom_country = this.getParameter('sysparam_from_country');
        var getto_country = this.getParameter('sysparam_to_country');

        var vk = "u_from1=" + getfrom;
        var vk1 = "u_to2=" + getto;
        var vk2 = "u_from1=" + getfrom_country;
        var vk3 = "u_to2=" + getto_country;
        gs.log(vk2 + 'test for rating' + vk3);

        var queastion_table = new GlideRecord("sys_decision_question");
        queastion_table.addQuery('decision_table', "4c171a6147878610f5539de4116d43f7");
        // queastion_table.addQuery('condition', vk);
        queastion_table.addEncodedQuery('conditionLIKE' + vk1);
        queastion_table.addEncodedQuery('conditionLIKE' + vk);

        queastion_table.query();
        if (queastion_table.next()) {

            var condition_value = queastion_table.getDisplayValue('answer');
            var risk_rating = condition_value.slice(45);
            gs.log('risk rating basic one = ' + risk_rating);

        }

        var ratings = [];
        var question_table_1 = new GlideRecord("sys_decision_question");
        question_table_1.addQuery('decision_table', "4c171a6147878610f5539de4116d43f7");
        question_table_1.addEncodedQuery('conditionLIKE' + vk2);
        question_table_1.addEncodedQuery('conditionLIKE' + vk3);
        question_table_1.query();
        if (question_table_1.next()) {

            var result = question_table_1.getDisplayValue('answer');
            var result1 = result.slice(45).toString();
            ratings.push(result1);
            gs.log('risk rating for the inbetween stop=' + result1);
            gs.log('all risk rating =' +ratings);

           
        }
       
        return result1;
    },
   

    type: 'Decision_table_risk_rating'
});
 
 
on submit client script
 
function onSubmit() {


    var mrvs = g_form.getValue('risk_rating_from_country');
    var actionName = g_form.getActionName();

    var multiRowValues = JSON.parse(mrvs);
    var abc = [];

    for (var i = 0; i < multiRowValues.length; i++) {

        var country_form = multiRowValues[i].from_country;
        var country_to = multiRowValues[i].to_country;
       // alert(country_form + ',venky' + country_to);

        var vk = new GlideAjax("Decision_table_risk_rating");
        vk.addParam('sysparm_name', 'getriskratings');
        vk.addParam('sysparm_from', g_form.getValue('from'));
        vk.addParam('sysparam_to', g_form.getValue('to'));
        vk.addParam('sysparam_from_country', country_form);
        vk.addParam('sysparam_to_country', country_to);

        vk.getXML(function risk(response) {

            var answer = response.responseXML.documentElement.getAttribute("answer");
           // alert('risk rating' + answer);
            var vj = abc.push(answer);
            alert('risk rating in country' + vj);

            // g_forn.setValue('risk_rating', answer);
             g_form.submit(actionName);

        });


    }

    // var vv = abc.toString();

    // alert('main risk' + vv);

}
1 REPLY 1

-O-
Kilo Patron

The current solution is the equivalent of loading a web page as many times as there are rows in the MRVS.

A way better approach would be to send the MRVS to the server (Ajax endpoint) in its entirety just once and get back the final result, doing all the processing server side.

The idea is to end up with as few server/Ajax calls as possible.

 

In that scenario the client code would be something like:

 

function onSubmit () {
	// Verify that the risk rating has been computed prior to this submit
	// Launch computing and exit returning false (to interrupt submit)
	// if verificatin fails
	if (!g_scratchpad.u_calculatedRiskRating) {
		calculateRiskRating();
		return false;
	}

	function JSONparse (json) {
		try {
			return JSON.parse(json);
		}
		catch (ex) {
			console.error('Risk rate calculation', ex);
			return {};
		}
	}

	function calculateRiskRating () {
		var vk = new GlideAjax('Decision_table_risk_rating');

		vk.addParam('sysparm_name', 'getriskratings');
		vk.addParam('sysparm_from', g_form.getValue('from'));
		vk.addParam('sysparm_to', g_form.getValue('to'));
		vk.addParam('sysparm_mrvs', g_form.getValue('risk_rating_from_country'));

		vk.getXMLAnswer(setRiskRating);
	}

	function setRiskRating (payload) {
		var data = JSONparse(payload);

		if (data.u_calculatedRiskRating) {
			// Set the flag that shows that risk rating has been complted
			g_scratchpad.u_calculatedRiskRating = data.u_calculatedRiskRating;

			// Save the risk rating in the appropriate field
			g_form.setValue('', g_scratchpad.u_calculatedRiskRating);

			// Re-submit the form
			g_form.submit();
		}
	}
}

 

 

As for the server side, I'm not sure what you are trying to do there, but that is not how Decision Tables "work".

There is an API one should use: DecisionTableAPI.

Something like:

 

var dt = new sn_dt.DecisionTableAPI();
var dtInputs ={
	'<decision table input name>': '<value for the given decision table input>',
	// Adding all the inputs defined in the Descition Table
}

var response = dt.getDecision('4c171a6147878610f5539de4116d43f7', dtInputs);
var riskRating = response.result_elements.<the decision table output column that contains the risk rating information>;

 

Of course you would need to replace <decision table input name> and <the decision table output column that contains the risk rating information> with the input and output names as defined in the Decision Table.

 

I would also consider returning numeric information as risk rating.

In order to calculate the highest risk you will need to be able to sort risks.

Using text values as risk levels that is not really possible; e.g. a hierarchy of High - Medium - Low sorted alphabetically will end up High - Low - Medium, or Medium - Low - High which makes it impossible to establish the correct risk ranking between High and Low, High and Medium and Low and Medium using the same logic.

However if one uses numeric values, e.g. 1 for High, 2 for Medium and 3 for Low, calculating the highest risk using the same logic is trivial.

If one must show labels in the Catalog Item, one could define the Risk field as a drop-down which would have as choice values the risk output values.