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

PDF Field Javascript Calculation to copy data from fields

New Here ,
Jun 04, 2024 Jun 04, 2024

I am trying to get a field in a PDF form to equal the value of another field, but if that field is blank, then I want it to to equal the value of the next field.  

For instance, what I have (that works for the first field provided data is entered): 

event.value = this.getField("field1").valueAsString; 

I need it to get data from another field IF field1 is blank, (for instance go to field2). 

 

Any help would be greatly appreciated. 

TOPICS
General troubleshooting , How to , JavaScript , PDF forms
952
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Jun 04, 2024 Jun 04, 2024
if(this.getField("field1").value)
{event.value=this.getField("field1").valueAsString}
else
{event.value=this.getField("field2").valueAsString}

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 ,
Jun 04, 2024 Jun 04, 2024

If you have a lot of these fields you can also do it like this:

 

 

var fields = ["field1", "field2", "field3"];
event.value = "";
for (var i=0; i<fields.length; i++) {
	if (this.getField(fields[i]).valueAsString!="") {
		event.value = this.getField(fields[i]).valueAsString;
		break;
	}	
}

 

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 ,
Jun 04, 2024 Jun 04, 2024
if(this.getField("field1").value)
{event.value=this.getField("field1").valueAsString}
else
{event.value=this.getField("field2").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
New Here ,
Jun 04, 2024 Jun 04, 2024

Thank you very much.  This worked well, but I should have fully ellaborated my scenario.  How could I continue this - for instance...if field2 is blank, get value for field3? 

It's much appreciated! 

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 ,
Jun 04, 2024 Jun 04, 2024
if (this.getField("field1").valueAsString!="") {
    event.value = this.getField("field1").valueAsString;
} else if (this.getField("field2").valueAsString!="") {
    event.value = this.getField("field2").valueAsString;
} else if (this.getField("field3").valueAsString!="") {
    event.value = this.getField("field3").valueAsString;
} // etc.
else event.value = "";
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 ,
Jun 04, 2024 Jun 04, 2024

If you have a lot of these fields you can also do it like this:

 

 

var fields = ["field1", "field2", "field3"];
event.value = "";
for (var i=0; i<fields.length; i++) {
	if (this.getField(fields[i]).valueAsString!="") {
		event.value = this.getField(fields[i]).valueAsString;
		break;
	}	
}

 

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 ,
Jun 04, 2024 Jun 04, 2024

Thank you, this response worked perfect.  Your 1st response may have an error somewhere as I could not get it to work.  

 

Thanks again - I appreciate your time!

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 ,
Jun 04, 2024 Jun 04, 2024

Both worked fine for me...

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 ,
Jun 04, 2024 Jun 04, 2024
LATEST

If all your fields are named in numerical order like this (field1, field2, etc.) and you want to populate the field with the first field in the order that is not blank you can use the following script:

for(var i=1; i<11; i++)

{

if(this.getField("field"+i).value)

{event.value=this.getField("field"+i).value; break;}

}

This is for 10 fields.  Change the 11 to the number of fields to check, plus 1.

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