Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

help with javascript

Explorer ,
Feb 04, 2021 Feb 04, 2021

I'm trying to adapt code to my needs but am having problems with it.

I have checkbox fields with numbers as export values and trying to sum those values if checkboxes are checked, here is what I have:

var total = "";
for (var i=1; i<=5; i++) {
if (this.getField("C"+i).valueAsString!= "Off") total = this.getField("C"+i).value;
}
event.value = total;

TOPICS
JavaScript
789
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Feb 04, 2021 Feb 04, 2021

Change the first line to:

var total = 0;

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 04, 2021 Feb 04, 2021

You don't need a script for this. At calculation use the option "Value is the sum of the following fields".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 04, 2021 Feb 04, 2021

I can't do that because I have other calculations in the same field.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 04, 2021 Feb 04, 2021

You're not adding up the values, but overwriting them.

Change this:

total = this.getField("C"+i).value;

To:

total += Number(this.getField("C"+i).valueAsString);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 04, 2021 Feb 04, 2021

I tried that already but I'm getting value as string e.g. 100200300 instead of 600.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 04, 2021 Feb 04, 2021

Change the first line to:

var total = 0;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 04, 2021 Feb 04, 2021
LATEST

Thank you, that worked.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines