Skip to main content
Participant
May 4, 2020
Answered

Fill in another text field based on previously entered text

  • May 4, 2020
  • 3 replies
  • 950 views

Hey there,

I am struggling a bit with my code, as I do not get it to work with my form. The current code I have is as follows:

var field1 = this.getField("Brand").valueAsString;

if (field1=="Samsung") event.value = "SM-G960F";

else if (field1=="Apple") event.value = "A2111";

else event.value = "";


The steps I followed were:

1) I created a text field named "Model" and made it "Read Only"

2) Under "Javascript", I created a new document JavaScript called "Model"

3) I filled in the code above, where the goal is as follows:

     - The code would start by retrieving the text field "Brand" to see what value has been filled in (i.e.   "Samsung"

    - If "Samsung" has been filled in, the "Model" field should display the string "SM-G960F"
    - If "Apple" was entered in the "Brand" field, the "Model" field should display the string "A2111"

    - If nothing has been filled in, then the field should stay empty.

The current error is as follows (I've cycled through quite a bit now):

 

TypeError: this.getField(...) is null
1:Document-Level:Model

 

But this seems strange to me, as the getField value is supposed to be null until it is filled in right? Or am I completely off with my thoughts?


I would be very grateful if one of you guys could help me out! Here are some screenshots as well:

 

This topic has been closed for replies.
Correct answer Bernd Alheit

Use the script as calculation of field "Model".

3 replies

Participant
June 1, 2020

Thanks both! Because of these answers, I realised my form wasn't in the correct mode (if anyone else is struggling: go to prepare form -> More -> Convert to form), which allowed me to se the calculation field. Thanks again!

Thom Parker
Community Expert
Community Expert
May 5, 2020

What it's telling you is that the field name is incorrect. It is the "getField" function that is returning null, not the value. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
May 5, 2020

Use the script as calculation of field "Model".