Calculating totals from mult-row variable set rows on a catalog

Fidel Fernandez
Tera Expert

Hi everyone.

I am a beginner in development. and need your help. Ihave this problem , i have to calculate the total in a variable set but i have two types fields (CAPEX and OPEX), i have calculate total (quantity * Price) to CAPEX and total (quantity * Price) to OPEX and see in a field and one fiel where total CAPEX+OPEX

 

find_real_file.png

I have this client script, it´s on change because i doing test if works good. 

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


    var mrvs = g_form.getValue("costes");
    var objList = JSON.parse(mrvs);

    for (var i = 0; i < objList.length; i++) {
        var cantidad = objList[i].cantidad;
        var coste = objList[i].coste;
        var tipo = objList[i].tipo_inversion;
        var total = (cantidad * coste).toFixed(2);

        var calculatedOpex;
        var calculatedCapex;

        alert(tipo);
        alert(cantidad);
        alert(coste);
        alert(total);

        if (tipo == "capex") { // Capex
            calculatedCapex += parseFloat(total).toFixed(2);
            alert("dentro de capex");

        } else {
            calculatedOpex += parseFloat(total.toFixed(2));
        }

    }
    alert("Total :" + calculatedCapex);

}

 

But only read the last type and doesn't add totals by type. Any idea?

Thank you 

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Hi Fidel,

You'll want to declare all variables outside of the for loop.  calculatedOpex and calculatedCapex are probably getting reset to empty each time it reads a new row of the MRVS as it is now.  You are running this script onChange of which variable?  This is a common issue when updating variables on the Catalog Item from an MRVS - how do you automatically trigger the script when the MRVS is updated - a row added, edited, or deleted. 

Hi Brad, thank you to write in the forum.

I try to explain , my english is not the best. Yes i want to  I running my script in onChange because i'm check if they execute, in future i will change it to Submit script.

 

find_real_file.png

I put variables outside of the loop, now the alert it´s the same unefines variable

find_real_file.png

find_real_file.png

 

The onChange/onSubmit makes sense for testing.  It's weird that you're getting undefined and yet you are getting a number after undefined.  Are these the correct numbers for the row(s) entered? If not, are the alerts on type, quantity, cost, and total correct?  If total is not correct, you may need to parseInt or parseFloat on quantity and cost in the calculation.

var total = parseInt(cantidad) * parseInt(coste);

I don't know if it matters in JavaScript, but it's always best to initialize the variables when you declare them, so try:

var calculatedOpex = 0;
var calculatedCapex = 0;
var totalsumm = 0;

Murthy Ch
Giga Sage

Hi @Fidel 

If you are using onsubmit it will be easy as suggested by Brad.

Sample script:

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var bA = 0;
    var bB = 0;
    var userDet = JSON.parse(g_form.getValue("user_details")); // mrvs internal name
    for (var i = 0; i < userDet.length; i++) {
        if (userDet[i].select_choice == "A") {
            bA += parseInt(userDet[i].add) * parseInt(userDet[i].multiply);
        }
        if (userDet[i].select_choice == "B") {
            bB += parseInt(userDet[i].add) * parseInt(userDet[i].multiply);
        }
    }
    g_form.setValue("budgeta", bA);
    g_form.setValue("budgetb", bB);
    return false;  //remove this line after testing..

}

Output:

find_real_file.png

Hope it helps.

 

Thanks,

Murthy

Thanks,
Murthy