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

How to combine values in one field when two other fields match

New Here ,
May 08, 2023 May 08, 2023

I am trying to make a form that arranges $ values when two fields match. The form has multiple rows (12 rows max) that calculate a $ value. Then at the end of each row, I have a drop-down (dropdown1) where you can select a fund source; FJC-HT, ARA-JC, and CB Cap are some examples.

 

At the bottom of the form, I want a cumulative total of the $ values for each selected fund source. I have another drop-down (dropdown2)with the same options as drowpdown1. I want to combine the 4 values of each row when these two drop-downs match. So if 3 rows have FJC-HT selected, I want the combined $ value of each row with FJC-HT in one field box next to dropdown2. Then if other sources are selected, they will be combined at the bottom respectively.

TOPICS
JavaScript , PDF , PDF forms
5.0K
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 ,
May 08, 2023 May 08, 2023

You can use something like this code as the custom Calculation script of the first total field you've described:

 

var total = 0;
for (var i=1; i<=12; i++) {
	if (this.getField("dropdown"+i).valueAsString=="FJC-HT") {
		total+=Number(this.getField("amount"+i).valueAsString);
	}
}
event.value = total;

 

Adjust the field names and text strings as needed.

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
New Here ,
May 12, 2023 May 12, 2023

I appreciate your support. I tried your suggested coding, but it doesn't produce the desired effect after applying my field names. I attached an image of the form I am working with to help better describe what I am hoping to achieve.

 

When an option is selected in "f1" - "f12", I want those options to be populated in fields "f1a" - "f12a", but I want the selections in "f1" - "f12" that are the same to combine in "f1a" - "f12a". Then I want the $ values in field "a1" - "a12" to follow the selected "f1" - "f12" options to the field next to matching "f1a" - "f12a"

 

So if, for example, "FJC-HT" is selected as an option, let's say "f1" "f2" and "f3". and its "a" values are $12, $3.75, and $5, respectively. I want "f1a" at the bottom to populate with "FJC-HT," and I want "s1" to show the combined $ value of the  "a"s. Then if other options are selected, I want them to match, and populated to "f2a" and on as needed.

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 ,
May 12, 2023 May 12, 2023

What happens when you use it, exactly? Are there any error messages in the JS Console?
Can you share the actual file with us?

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
New Here ,
May 12, 2023 May 12, 2023

I tried it a couple of ways but didn't get any error messages. I've attached the form file I am working with.

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 ,
May 12, 2023 May 12, 2023

Press Ctrl+J to open the JS Console and you'll see an error message appear after you edit the value of any field. I also don't see where you applied the code I provided...

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 ,
May 12, 2023 May 12, 2023

I made some minor adjustments to the code and it works fine:

 

var total = 0;
for (var i=1; i<=12; i++) {
	if (this.getField("f"+i).valueAsString=="FJC - HT") {
		total+=Number(this.getField("a"+i).valueAsString);
	}
}
event.value = total;
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
New Here ,
May 12, 2023 May 12, 2023

I see what I was doing wrong with the names I was using to replace with your original placeholders. Thank you!

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 ,
May 12, 2023 May 12, 2023

Note I also adjusted the value it looks for, from "FJC-HT" (as you originally wrote) to "FJC - HT" (as it actually appears in the file). Even a single space out of place would cause it to not work properly.

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
New Here ,
May 12, 2023 May 12, 2023

The copy I provided was a code-free version. But I was testing your provided code on the first "sub" box of the bottom part of the form. If you are looking the Field names, its the one labeled "s1"

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 ,
May 12, 2023 May 12, 2023

Yes, that's where I put the code above and it worked fine.

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
New Here ,
May 15, 2023 May 15, 2023
LATEST

Can "FJC -HT" be replaced with some kind of code that represents any of the available options in the drop-down? That way I don't have to have a box with this code for every possible option.

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