Skip to main content
May 14, 2016
Question

getField returns null in document level javascript

  • May 14, 2016
  • 5 replies
  • 6251 views

The code shows as below:

    app.alert(1);

    app.alert(this.getField("IsModified").value);

    app.alert(2);

I put the script in document level. When I finish editing the script, it gives me 3 popup as expected.

After I closed the PDF and reopen it, it gives me the first popup ONLY.

I checked the debugger. It gave me the error as below:

     TypeError: this.getField(...) is null

     3:Document-Level:alertTest

I can tell the problem is on this.getField("IsModified"). But why this happens? And how should I fix it?

This topic has been closed for replies.

5 replies

Participant
July 15, 2016

Hello,

I am running into what appears to be the same problem.  I have this document level script that returns "null" when I perform a getField when I first open the document.  If however I interact with another part of the document OR I put it in "Prepare Form" mode in Acrobat DC and then back into preview, it works just fine.  So I know the field name is correctly spelled.

It almost appears as if the field object doesn't get created upon document launch, but that something triggers the field creation later. 

Any thoughts?  Did you resolve this issue?

Cheers

Inspiring
July 15, 2016

To have a field object you need an open PDF form. Do you have an open form at the yime the script is running?

Inspiring
May 15, 2016

Try the following script:

app.alert(1);
var oField = this.getField("IsModified"); // get field object;
// see if there was an error accessing the field object;
if(oField == null) {
app.alert({cMsg: "Error accessing field \"IsModified\".\nPlease check field name, spelling, and capitialization.",
nIcon: 1,
nType: 0,
cTitle: "Field Error Condition"
});
} else {
    app.alert(this.getField("IsModified").value);
}
    app.alert(2);

This script check the return value for the getField method and issues an alert if there was a problem accessing the provided field name. Check to make sure the field really exist.

I add a document level function that I use in place of the this.getField method which includes the check of the return value for the this.getFeild method.

try67
Community Expert
Community Expert
May 14, 2016

How (and where) did you create the field?

Inspiring
May 14, 2016

As a test, what happens if you place the same code in a button and click the button?

Legend
May 14, 2016

It essentially means that a field named "IsModified" does not exist.

Carefully check spelling. Maybe locate the field, open its properties dialog, and copy the field name from there, and paste it into the code.

Hope this can help.