Global and local variables
JavaScript has global variables and local variables. A local variable can only be accessed in the scope of the method in which it is created. You use the var identifier to create a local variable in JavaScript, as shown in the following line of code:
var localCounter = 0;
To create a global variable, omit the var identifier, as shown in the following line of code:
globalCounter = 0;
A global variable in JavaScript is visible to all other JavaScript code that executes in the same process. For example, use a global variable to count the detail rows in a table by first creating a global variable in the onCreate( ) method of the table, as shown in the following line of code:
rowCount = 0;
Because rowCount is global, the onCreate( ) method of the detail row can access and increment it, as shown in the following line of code:
rowCount++;
To store global variables, use the reportContext.setGlobalVariable( ) method. This approach supports passing the global variable to a Java event handler.
If a report is being executed using two separate processes, such as RunTask and RenderTask, a global variable is only available to both processes if set using the reportContext.setPersistentGlobalVariable( ) method. This method writes the value of the variable to the report document before rendering. If you are setting a global variable in the initialize method, remember that this event fires many times when using two processes.