Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Calculate two fields and autopopulate sum

bindu999
Tera Contributor

Hi,

 

    I'm using client script to calculate the value of a+b and get total sum value in the single line text box. This script is not working for me. can you help me

 I'm using 2 select box'es 

a Select Box has option yes or no  (Yes value as 10 ; No Value as 5)

b Select Box has option yes or no (Yes value as 10 ; No Value as 5)

 

I'm using C as a single line text box I need to get the score of what the user selects.

If Select Yes in A; Yes in B; 

It should autopopulate C as 10

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
var xyz ='';
var X = g_form.getValue('a');
alert('a');
var Y = g_form.getValue('b');
var Z = g_form.getValue('c');

xyz = X+Y+Z;

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

return;
}


}

 

Please Help!!

 

Thank you!!!

1 ACCEPTED SOLUTION

Hi @bindu999 

 

Finally Im here with an answer,

That is you have to write two Onchange client scripts() for two variables ie, var1,var2.

I have tried in my PDI its working as you expected.

Im posting the screenshots as well..

Let me know if you still facing difficulty..

 

Code:

Onchange()

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

if (newValue != oldValue && newValue != '') {

 

var f1 = g_form.getValue('variable1');

var f2 = g_form.getValue('variable2');

if (f1 != '' && f2 != '') {

g_form.setValue('result', parseInt(f1) + parseInt(f2));

}

}
}

 

PFA:

find_real_file.pngfind_real_file.png

 

Thanks,

Murthy

Thanks,
Murthy

View solution in original post

21 REPLIES 21

joshvanharn
Kilo Guru

It looks like you're executing during loading, which won't work.

 

Updated code:

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

 var xyz ='';
 var X = g_form.getValue('a');
 alert('a');
 var Y = g_form.getValue('b');
 var Z = g_form.getValue('c');

 xyz = X+Y+Z;

 g_form.setValue('score', xyz);
 alert('xyz');
}

Hi Josh,

 

    I tried this script but didn't work. 

A Select Box with options None, Yes, No  (Yes Value 10, No Value 5)

B Select Box with options None, Yes, No  (Yes Value 10, No Value 5)

C Select Box with options None, Yes, No  (Yes Value 10, No Value 5)

 

Score Single Line Text box

Murthy Ch
Giga Sage

Hi @bindu999 

Try this:

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

var xyz ='';
var X = g_form.getIntValue('a');
alert('a');
var Y = g_form.getIntValue('b');
var Z = g_form.getIntValue('c');

xyz = X+Y+Z;

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

}

Thanks

Thanks,
Murthy

Mahesh Kumar3
Giga Guru

Can you try this script:

Your script has some error:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
	
    var X = g_form.getValue('a');
    var Y = g_form.getValue('b');
    var Z = g_form.getValue('c');

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

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

}