Calculate the sum of 3 fields and autopopulate total field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2022 08:07 AM
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?
- Labels:
-
Service Portal Development
- 1,887 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 06:39 AM
Do you still need help?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2022 04:15 PM
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
Step2: Enter c2
Step 3: Enter c3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2022 04:51 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 08:59 AM
while creating a calculation using onchange script which field do we need to select?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 07:01 AM
Do you still need help?