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

help with javascript

Explorer ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

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

Views

429

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Feb 04, 2021 Feb 04, 2021

Change the first line to:

var total = 0;

Votes

Translate

Translate
Community Expert ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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);

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Change the first line to:

var total = 0;

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Thank you, that worked.

Votes

Translate

Translate

Report

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