Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Creating global variables

Avatar

Level 4

Hi,

I am searching for the best way to create global variables so that the script can work on all adobe reader versions . I tried gobal.varibaleName at the initialize event of the form, but it is only working in the earlier versions of designer. I tried using script variables, but it always initializes the variable to string value. I have wrote some functions on the script object, and every time any functions fails, a  global variable count gets incremented. At the potsave event of the form if the count value is greater then 0 , a simple warning message is displayed . Now how to declare and initialize and increment this count variable so that the script can work on all the versions of adobe reader ?

1 Accepted Solution

Avatar

Correct answer by
Level 10

You need to change your code little bit..

First,

All Global Variables are String Type. If you use a "+" sign to add a number it will treat the value as string and append at the end.

So if you want to do numerical calculations on the global variables first you need to convert them to the right type.

In the below code I am parsing the value to an Integer type first before I increment it.

Second,

You have created a variable of name "count" which could be a reserved name. Try to avoid the reserved names for variables. So I created a variable "intCount".

Third,

After incrementing  value if you want to assign the value back to a Global variable you need to conver it to String type. (the opposite way like you did earlier in step 1 by converting String type to Integer)

var a = parseInt(intCount.value) + 1 ; //Step 1


intCount.value = a.toString();   //Step 2
var b = parseInt(intCount.value) + 1;  //Step 3
intCount.value = b.toString();  //Step 4

Thanks

Srini

View solution in original post

4 Replies

Avatar

Former Community Member

Global variables can be defined in the Form Properties page (off of the file menu). Click on the variales tab and you can define your global variables there. Note that when you want to access your variables from your script you woudl use the variableName.value

Another way is to define your variable in a scriptobject but the declaration must be outside of the function (usually the 1st thing). When teh form loads the script is executed and the var is defined and created at that time. To access this variable you will need scriptObjectName.variableName. Normally people use this technique for arrays or complex objects (other than simple types).

Hope that helps

Paul

Avatar

Level 4

Thanks for the reply Paul . I created one global variable count from the variable tab and initilized it to 0. Now i am trying it to assign a different value or change it's value through scripting . for eg - var a = Number(count.value) + 1 ; Step 1

                                                               count.value = a;   Step 2

                                                               var b = Number(count.value) + 1;  Step 3

                                                               count.value = b;  Step 4

After the step 2 and 4 the value of the count should be 1 and 2 respectively, but it is 0 . Cannot i change the assigned value of a variable declared and initialized through variable tab through javascripting and then make the new value persistent ?

Avatar

Former Community Member

I think that you are getting an error in your script when you try to assign the variable a numberic value. If you are in acrobat you can hit Ctrl-J to see the javascript console and you will see the error. I am not 100% sure but it seems that it can only take a string. I have done the same thing using a different technique where we create a scripting object and define the global variable in there (as a numeric). When a form loads the scripting object is executed and any var definitions are made global. The way we access the var is a little different. I created a sample for you (see the attachment) ...have a look and let me know if this is acceptable. In the mean time I will validate the variables tab in the Form Properties.

Paul

Avatar

Correct answer by
Level 10

You need to change your code little bit..

First,

All Global Variables are String Type. If you use a "+" sign to add a number it will treat the value as string and append at the end.

So if you want to do numerical calculations on the global variables first you need to convert them to the right type.

In the below code I am parsing the value to an Integer type first before I increment it.

Second,

You have created a variable of name "count" which could be a reserved name. Try to avoid the reserved names for variables. So I created a variable "intCount".

Third,

After incrementing  value if you want to assign the value back to a Global variable you need to conver it to String type. (the opposite way like you did earlier in step 1 by converting String type to Integer)

var a = parseInt(intCount.value) + 1 ; //Step 1


intCount.value = a.toString();   //Step 2
var b = parseInt(intCount.value) + 1;  //Step 3
intCount.value = b.toString();  //Step 4

Thanks

Srini