Copy link to clipboard
Copied
I know this is a commonly seen error where a JavaScript can't find a particular field (I've searched the forums 😉 )
Been staring at my code trying to figure out why it is generated, and hoping one of you will spot the issue immediately.
Scenario:
What's happening:
Here's what I've got that generates the above error (doing a simple test with just two questions at the moment only, as easier for me to get the script sorted).
var arrayQuestions1 = ["Text from Q1", "Text from Q2"] //read
var arrayResults1 = new Array(); // write
for (q=0; q < 2; q++) {
var QNum = q + 1; // is a number
var QName = "Q" + QNum; // is a string that matches field name (Q1, Q2 etc.)
var QValue = getField("QName").value; // get field Q1, Q2 value
if (QValue < 3) {
arrayResults1
= arrayQuestions1+ "\n";} else {
arrayResults1
= "";}
}
this.getField("Results1").value = arrayResults1[0] + arrayResults[1];
Use Name, not QName. QName is an internal function.
Copy link to clipboard
Copied
This is a small sample PDF I put together for testing and development purposes. It uses the second version of the script that I added to this post. https://dl.dropboxusercontent.com/u/28545642/forums-radio-value-export-to-field-small-sample.pdf
Copy link to clipboard
Copied
Still puzzling on this one, I'm thinking the issue is definitely related to the QName variable.
Tried to incorporate the quotes as part of the variable value, so that varQValue getField("Q1").value etc. but still getting the same error
Here's latest JavaScript I tried:
var arrayQuestions1 = ["Text from Q1", "Text from Q2"] //read
var arrayResults1 = new Array(); // write
for (q=0; q < 2; q++) {
var QNum = q + 1; // is a number
var QName = "\"Q" + QNum +"\""; // is a string that matches field name (Q1, Q2 etc.)
var QValue = getField(QName).value; // get field Q1, Q2 value
if (QValue < 3) {
arrayResults1
= arrayQuestions1+ "\n";} else {
arrayResults1
= "";}
}
this.getField("Results1").value = arrayResults1[0] + arrayResults1[1];
Copy link to clipboard
Copied
Use:
QName = "Q" + QNum;
Copy link to clipboard
Copied
Hmm.. I've tried that, that still gives the same error. Have updated the attached sample PDF file as well. This is the latest I've tried:
var arrayQuestions1 = ["Text from Q1", "Text from Q2"] //read
var arrayResults1 = new Array(); // write
for (q=0; q < 2; q++) {
var QNum = q + 1; // is a number
var QName = "Q" + QNum; // is a string that matches field name (Q1, Q2 etc.)
var QValue = getField(QName).value; // get field Q1, Q2 value
if (QValue < 3) {
arrayResults1
= arrayQuestions1+ "\n";} else {
arrayResults1
= "";}
}
this.getField("Results1").value = arrayResults1[0] + arrayResults1[1];
It's doing my head in, I'm sure it's related to how I pass the filename into the getField function.
If I were to write it it out 'long-hand' I'd use
var QValue = getField("Q1").value;
Which made me think it is to do with passing those quotes somehow.
I don't script too often to be honest so always rusty whenever I do need to resort to my scripting skills
Copy link to clipboard
Copied
Use Name, not QName. QName is an internal function.
Copy link to clipboard
Copied
Wow you are a legend @Bernd!!!! I would have never figured that one out by myself. Thank you so, so much.
Interesting also to see that this was specific to Acrobat DC, I indeed failed to mention which version of Acrobat I was using.
Thanks so much to all who responded.
Working Script looks like this:
var arrayQuestions1 = ["Text from Q1", "Text from Q2"] //read
var arrayResults1 = new Array(); // write
for (q=0; q < 2; q++) {
var QuestionNum = q + 1; // is a number
var QuestionName = "Q" + QuestionNum; // is a string that matches field name (Q1, Q2 etc.)
var QuestionValue = getField(QuestionName).value; // get field Q1, Q2 value
if (QuestionValue < 3) {
arrayResults1
= arrayQuestions1+ "\n";} else {
arrayResults1
= "";}
}
this.getField("Results1").value = arrayResults1[0] + arrayResults1[1];
Copy link to clipboard
Copied
It's working fine for me... After pressing the button the text field is populated with:
Text from Q1
Text from Q2
Copy link to clipboard
Copied
It doesn't work with version DC.
Copy link to clipboard
Copied
It does in XI. So the problem is most likely a bug in DC...
Copy link to clipboard
Copied
If you do as Bernd suggested and change the name of the QName variable to something else (e.g., "QName1"), it will work. What's happening is Acrobat is not allowing you to overwrite the existing function named QName, which is a good thing and not a bug.