Skip to main content
Inspiring
December 4, 2022
Answered

Calculation issue

  • December 4, 2022
  • 1 reply
  • 2280 views

Hello, Good Day

I have cost fields want to calculate, tried but failed, hopefully experts help me as always. I am using code:

var v1 = this.getField("Text22").valueAsString;
var v2 = this.getField("Text23").valueAsString;
var v3 = this.getField("Text24").valueAsString;
var v4 = this.getField("Text25").valueAsString;
var v5 = this.getField("Text26").valueAsString;
var v6 = this.getField("Text27").valueAsString;
var v7 = this.getField("Text28").valueAsString;
var v8 = this.getField("Total").valueAsString;
if (v1=="" && v2=="" && v3=="" && v4=="" && v5=="" && v6=="" && v7=="") event.value = "";
if (v1=="not applicable" && v2=="not applicable" && v3=="not applicable" && v4=="not applicable" && v5=="not applicable" && v6=="not applicable" && v7=="not applicable") event.value = "";

else {
var Text22 = Number(v1);
var Text23 = Number(v2);
var Text24 = Number(v3);
var Text25 = Number(v4);
var Text26 = Number(v5);
var Text27 = Number(v6);
var Text28 = Number(v7);
var Total = v1 + v2 + v3 + v4 + v5 + v6 + v7;
}


file is also attached. Thanks

This topic has been closed for replies.
Correct answer Nesa Nurani

Since you want to have text (not applicable) and numbers, you can't format the field as number, so 17,500 is considered a string and not a number.

You can use this script:

var fields = ["GD","DD","NN","AF","PF","BC","GR"];
var total = 0;
var str = [];
for(var i in fields){
var f = this.getField(fields[i]);
if(f.valueAsString != "" && f.valueAsString != "not applicable")
str.push(f.valueAsString.replace(",",""));}
for(var j in str){
total += Number(str[j]);}
if(total == 0)event.value = "";
else
event.value = total;

This should work with numbers and numbers with a comma (17000 or 17,000)  or 'not applicable'.

1 reply

try67
Community Expert
Community Expert
December 4, 2022

There are multiple issues here.

The main ones are:

- You did not apply the Total variable to event.value, so it doesn't appear in the field.

- You did not use the TextX variables you've declared for the sum, but the vX variables, which can be string.

- You did not account for the "not applicable" strings when converting the values to a Number. They will return NaN (Not a Number). Also, you must manually remove the commas from your numbers before adding them up.

Inspiring
December 4, 2022

Thank you very much for the guidance, I am a beginner and trying to learn here, don't know much about coding. Please I need your help to solve this. If I do the sum feature without code it works fine but as soon as I add the comma (17,500) there the calculation goes wrong, too much confused how will be solved, here are the two files attached one with the coding and the other with the simple sum feature, I hope Someone will help me.

try67
Community Expert
Community Expert
December 4, 2022

If you set the Format option of the fields to the right pattern it will add the commas automatically and the calculations will work.