Calculate the sum of 3 fields and autopopulate total field

Snehita katta
Mega Contributor

I have been trying to calculate the sum of 3 fields and auto-populate a total field with the following OnChange script: 

   var X = g_form.getValue('c1');
    var Y = g_form.getValue('c2');
    var Z = g_form.getValue('c3');

    var xyz = parseInt(X) + parseInt(Y) + parseInt(Z);

    g_form.setValue('total', xyz); 
    alert(xyz);

However, this isn't yielding any result but it is working with OnSubmit Client Script. 

Can someone pls help me where I'm going with this?  

 

9 REPLIES 9

Do you still need help?

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Snehita,

Instead of parseInt(), use Number(). Create the following onChange script on each of variable c1, c2, and c3.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var X = g_form.getValue('c1');
    var Y = g_form.getValue('c2');
    var Z = g_form.getValue('c3');

    var xyz = Number(X) + Number(Y) + Number(Z);

    g_form.setValue('total', xyz);

}

Execution result

Step 1: Enter c1

find_real_file.png

Step2: Enter c2

find_real_file.png

Step 3: Enter c3

find_real_file.png

 

 

Thank you very much for your valuable input. The script finally worked with your correction and  also as Mr.John mentioned declaring the initial value of zero for each of these variables. 

Nanibabu illa
Tera Contributor

while creating a calculation using onchange script which field do we need to select?

 

Do you still need help?