These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Global variables & not defined messages

David Dunn
Registered: Oct 28 2010
Posts: 116
Answered

I have a number of global variables employed in a series of custom calculations attached to various fields. I have placed the field calculations order to the correct sequence. The calculations work as intended, no delays due to incorrect ordering of calculations. However, when I open the document, and only when I open the document, I get a console message referencing some of the global variables as "not defined."
 
This first occurred when the variables were defined in a document level script that did nothing more than define all of the variables. Since some of the variables were employed in a document function, I thought perhaps I should have defined the variables in the function rather than a separate script, so I moved the variable definitions into the function and deleted the separate script defining the global variables. After doing this, I get the same result: all of the forumlas calculate correctly, but when I open the document, the console message reappears, referencing global variables as not defined even though formulas in which they are used are functioning properly.
 
Help, please. Thank you.

David D

My Product Information:
Acrobat Pro 9.4, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
What is the exact text of the message?
David Dunn
Registered: Oct 28 2010
Posts: 116
ReferenceError: gRdoApplyCommission is not defined
3:AcroForm:txt.BrokerPOC:Calculate
gRdoApplyCommission is not defined
1:AcroForm:num.SS.Credit.EM2:CalculateException in line 1 of function top_level, script AcroForm:num.SS.Credit.EM2:Calculate

My comment in re the above: gRdoApplyCommission IS defined and the spelling and case of each instance is exactly as defined.


David D

David Dunn
Registered: Oct 28 2010
Posts: 116
George, the problem seems solved now but I don't know why. I removed the global variable definitions from the document level function and placed those global variables into their own separate Document level script. I concurrently received another error message referencing a simple document level function that had nothing to do with what I was working on. Since I was no longer using that unrelated function, I deleted it. Now all seems to be working just fine.

David D

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Are you sure you were not redefining the variable each time the function was called or the variable only existed as long as the function was active.

I have found that testing for the existence of a global variable before trying to use it is most helpful. If it does not exist, then I can define it if needed.

See Acumen Journal for August 2004.

George Kaiser

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
Note that variables that you want to be document-global need to be declared outside of a function. If they're defined inside of a function, they are local to that function. If declared outside of a function, they are document-global as soon as the line where they are declared is executed. Since code in a document-level JavaScript are executed when the document is opened, they will become available when the document is opened.
David Dunn
Registered: Oct 28 2010
Posts: 116
Elements of both of your comments were at play I believe. When I first experienced the problem the global variables were declared outside of the function, and were not re-defined inside the function. I moved the global variables inside the function and deleted the separate document level script declaring them, not realizing I was doing the wrong thing, as explained by George Johnson. It quickly became apparent that was the wrong thing to do, so I removed them from the function and again declared them in a separate document level script.

I concurrently received another error message referencing a separate document script defined to "calculateNow()" (so that calculations were performed each time the document was opened), and since I had no need of that, I deleted it, and all was well. I suspect the document level "calculateNow()" script was executing on document open before the others and throwing the error bc it wasn't finding the global variables.

That brings up 2 more questions: (1) what is the order in which document level scripts execute (in the order they appear in the document Java Script Functions dialog box?); and (2) if a document level function defines a math calculation that field-level calculations depend on, does the function calculation always execute before the field level math calculations? (I don't see a reference to function math formulas in the dialog box that allows you to re-order field level calculations.)

David D

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
That sounds like a likely explanation.

Acrobat seems to order document-level scripts alphabetically in the PDF, and executes them in that order. I've never found that documented anywhere, only discovered it empirically.

Scripts attached to a Page Open action execute after the document-level scripts are executed, so that would be an option for placing code that you want to execute after any document-level code.
David Dunn
Registered: Oct 28 2010
Posts: 116
Good to know.

As to the second question, if for example the formula Z = (X * Y) is defined as a document level function, and "X" and "Y" are input into fields "txtA" and "txtB" after document open, and a formula in a field level calculation attached to field "txtC" needs "Z" as one element of its formula, is Z calculated and returned by the function as soon as X and Y are supplied, so that Z is available for the second formula? My focus in this question is on your statement that code in a document level script executes when the document is opened, yet in my example the figures needed to calculate "Z" are not supplied until after the document is opened.

David D

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
When I say that document-level code is executed, it means that any declared variables are initialized and then available for later use by other code. Think of a function that's defined in a document-level JavaScript as another document-global variable (because it is). Any functions defined in document-level JavaScript are similarly available for later use by other code.

These two are equivalent and are called the same way:

var myfunc = function(x, y) {
return x * y;
}

function myfunc(x, y) {
return x * y;
}

// Returns 42
var Z = myfunc(6, 7);


In your example, the function that returns the value Z can be called in the calculate event of txtC, passing it the values of txtA and txtB. When the document-level code that defines the function is executed, it doesn't calculate anything at that time, it just becomes available for later use by other code.
David Dunn
Registered: Oct 28 2010
Posts: 116
Thank you. That is clear.

David D