Sum of variables in variable set & to print it outside variable.

aamir khan
Tera Contributor

Hi members, 

 

I have a requirement to sum up the cost value (variable set) and to display it in the Total Cost variable (outside the variable set). I have found couple of similar old posts and members marked it as helpful but it didnt work  at all by onchange client scipt etc...

 

Anyone can help me out will be thankful.

 

aamirkhan_0-1733919853280.png

 

1 ACCEPTED SOLUTION

JenniferRah
Mega Sage

I put this in a Client Script and it worked for me. I assumed your Cost variable is named "cost".

    var mrvsName = 'yourmrvsname';
    var mrvs = g_form.getValue(mrvsName);
	var total_cost = 0;
    var objList = JSON.parse(mrvs); //The option values are captured as a JSON string, which we parse, creating an array
    for (var i = 0; i < objList.length; i++) { //Iterate through array
        var row = objList[i];
        total_cost += parseInt(row.cost);
    }
	g_form.addInfoMessage("Total cost is " + total_cost);

 

View solution in original post

2 REPLIES 2

JenniferRah
Mega Sage

I put this in a Client Script and it worked for me. I assumed your Cost variable is named "cost".

    var mrvsName = 'yourmrvsname';
    var mrvs = g_form.getValue(mrvsName);
	var total_cost = 0;
    var objList = JSON.parse(mrvs); //The option values are captured as a JSON string, which we parse, creating an array
    for (var i = 0; i < objList.length; i++) { //Iterate through array
        var row = objList[i];
        total_cost += parseInt(row.cost);
    }
	g_form.addInfoMessage("Total cost is " + total_cost);

 

Thanks Jennifer, 

Your solution helped me to achieve the required work.