kevin-kilroy
3/24/2020 - 12:47 PM

Working version of Assessment Score Calculation

Script Include

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

    calculateScore: function() {
        // get the paramters from passed from the client script
        var totalApps = this.getParameter('sysparm_totalAppplications');
        var genAvail = this.getParameter('sysparm_gaStatus');
        var serviceCap = this.getParameter('sysparm_capabilityStatus');
        var totalDevs = this.getParameter('sysparm_totalDevelopers');
        var profitGen = this.getParameter('sysparm_profitGeneration');
        var message = 'totalApps: ' + totalApps + ' genAvail: ' + genAvail +
            ' serviceCap: ' + serviceCap + ' totalDevs: ' + totalDevs + ' profitGen: ' + profitGen;
        gs.addErrorMessage(message);

        var score = 0;
        // get the scrore for each field based on the value passed it
        var ta = this._getScore(totalApps, 'total_applications');
        var ga = this._getScore(genAvail, 'generally_available_status');
        var sc = this._getScore(serviceCap, 'service_capability_status');
        var td = this._getScore(totalDevs, 'total_developers');
        var pg = this._getScore(profitGen, 'profit_generation');
        // add up all the results
        score = ta + ga + sc + td + pg;
        return score;
    },
    _getScore: function(value, fieldName) {
        var score = 0,
            propName = 'x_12350_playground.sa_score_' + fieldName,
            propValues = gs.getProperty(propName).split(','),
            getValue = value - 1;

        value = parseInt(value, 10);
        // get the value from the array and make it an integer
        score = parseInt(propValues[getValue], 10);
        return score;

    },

    type: 'ServiceAssessmentUtil'
});

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    alert(newValue);
    var calculateScore = new GlideAjax('ServiceAssessmentUtil');
    calculateScore.addParam('sysparm_name', 'calculateScore');
    calculateScore.addParam('sysparm_totalAppplications',  newValue);
    calculateScore.addParam('sysparm_gaStatus', g_form.getValue('ga_status'));
    calculateScore.addParam('sysparm_capabilityStatus', g_form.getValue('service_capability_status'));
    calculateScore.addParam('sysparm_totalDevelopers', g_form.getValue('total_developers'));
    calculateScore.addParam('sysparm_profitGeneration', g_form.getValue('profit_generation'));
    calculateScore.getXML(updateScore);

    function updateScore(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
        var result = parseInt(answer, 10);
        g_form.setValue('assessment_score', result);
    }
}