Skip to main content
Terbo
Known Participant
December 5, 2017
Answered

Auto-populate based on one of two fields

  • December 5, 2017
  • 2 replies
  • 1471 views

I need to auto-fill a calculated field from either of two fields.  If location A IS NOT blank, is should be auto-populated from location A.  If Location is A IS blank, I need it to calculate from location B.  I already know the script for the calculation.  Is there a way to do this?

This topic has been closed for replies.
Correct answer try67

Ah, OK. Use this code, then:

var a = this.getField("A").valueAsString;

if (a!="") event.value = a;

else {

    // insert code that uses the value of field B here

}

2 replies

Terbo
TerboAuthor
Known Participant
December 5, 2017

Sorry if that was unclear.  I know the calculation required to use location B if A is blank.  I don't know how to choose A or B based on the content of A.

Thom Parker
Community Expert
Community Expert
December 5, 2017

Try just gave you the code for selecting A if A is not blank.

// This first line gets the value of A

var a = this.getField("A").valueAsString; 

// this line sets the target field to the value of A if A is not blank
if (a!="") event.value = a; 

// This is an else statement, it runs the code in the block below, if the first condition is not met, i.e. A is blank
else
    // insert code that uses the value of field B here 

You did say you know the code for the case where B is selected?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
December 5, 2017

Your question is not clear. If you already know the calculation script to do it, then what's the problem?

Terbo
TerboAuthor
Known Participant
December 5, 2017

IF I choose field B, I need to perform a calculation on the entry in field B. That is the calculation I know. If I choose field A, I just need to import Field A as is. What I don’t know is how to get my field to choose A (if not blank) or B (if A is blank).

- Thanks

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 5, 2017

Ah, OK. Use this code, then:

var a = this.getField("A").valueAsString;

if (a!="") event.value = a;

else {

    // insert code that uses the value of field B here

}