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

Combine fields only if field has content

Community Beginner ,
Apr 22, 2019 Apr 22, 2019

I have 30 comment fields in my form and I want to consolidate it to 1 field.
However, users have an option to just use how many comment fields they like. That means 1-30.

The comment fields have a default value, "Comment -1", "Comment-2" etc.

Since the fields have default value, is there a way to combine items only if it has content, meaning users have put in a real content in the field and not just the default value?

Right now this is how the form looks like. The script I have is only written up to comment 4 that's why comments 5-30 is not visible.

I want the summary field to show only comments 1 and 2, with the "comments#".

Thank you in advance.

TOPICS
PDF forms
1.9K
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 ,
Apr 22, 2019 Apr 22, 2019

You can use this code as the custom calculation script of your text field:

var text = "";

for (var i=1; i<=30; i++) {

    var s = this.getField("Comment-"+i).valueAsString;

    if (s!="") text+="Comments-"+i+": " + s + "\r";

}

event.value = text;

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 ,
Apr 22, 2019 Apr 22, 2019

You can use this code as the custom calculation script of your text field:

var text = "";

for (var i=1; i<=30; i++) {

    var s = this.getField("Comment-"+i).valueAsString;

    if (s!="") text+="Comments-"+i+": " + s + "\r";

}

event.value = text;

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 Beginner ,
Apr 22, 2019 Apr 22, 2019

Hey. Thank you for responding. Should I use this code for the summary field? I tried pasting this script and looks like it didn't work.

Might be another stupid question but the current field names of my comment fields are t_1, t_2, t_3, t_4 etc. Should I change the names in order for this code to work?

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 ,
Apr 22, 2019 Apr 22, 2019

Yes, as the custom calculation script of the summary field.

OK, in that case you need to change either the names of the fields or the code.

Doing the latter is much easier... Simply change this:

var s = this.getField("Comment-"+i).valueAsString;

To:

var s = this.getField("t_"+i).valueAsString;

PS. When a code doesn't work the first thing you should do is look in the JS Console (Ctrl+J), as it will report any errors that happened while executing it, like an incorrect field name.

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 Beginner ,
Apr 22, 2019 Apr 22, 2019

Gotcha. This script worked however, I needed to delete the default value "Comment -#:".. We need to have that in every comment field though. So users know that they can type in a comment per box. If I don't delete the default value then it will look like this.

Any idea on how to make that happen? Still have the "Comment -#:" per comment field but it won't duplicate in the summary if it has content and it won't show up if comment field wasn't used.

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 ,
Apr 22, 2019 Apr 22, 2019

Just remove this part from the code: "Comments-"+i+": " +

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 Beginner ,
Apr 22, 2019 Apr 22, 2019

Thank you so much for your help.

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 ,
Apr 22, 2019 Apr 22, 2019

By the way, this should not happen if the fields are really empty. It seems your fields already contains the text "Comments-XX:"...

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 Beginner ,
Apr 22, 2019 Apr 22, 2019

We just decided to use this code that you gave and ended up deleting the default values in the comment fields.

var text = ""; 

for (var i=1; i<=30; i++) { 

    var s = this.getField("t_"+i).valueAsString;

    if (s!="") text+="Comments-"+i+": " + s + "\r"; 

event.value = text; 

Thank you again so so much!

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 ,
Apr 22, 2019 Apr 22, 2019
LATEST

That's probably a better approach.

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