Expand my Community achievements bar.

Changing Global Variables using Javascript

Avatar

Level 10

Hi,

I have a project where I need to have lots of signature fields on a single form. I don't want to use a signatureField because this will increase the file size every time a signatureField is signed. Also client does not want digital signatures.

I set up three user variables (for both usernames and passwords), using Form/Properties/Variables.

I then set up a dialogue box on the click event of a button, which requests the username and password. If these match the global variables, then the associated textField is populated with the user's credentials. I got this to work OK.

The problems stated when I gave the user the ability to change their password. This would be an important, as the form would not need to come back to me to hardwire in the new password.

One of the global variables is "Pass1".The following script changed the value of the variable, but would not hold the change when the form was reopened:

     Pass1.value = AHSC02.strnewP;

".value" shouldn't work, but it did???

So I tried different versions of ".rawValue", but with no sucess:

     Pass1.rawValue = AHSC02.strnewP;

     form1.Pass1.rawValue = AHSC02.strnewP;

     form1.variables.Pass1.rawValue = AHSC02.strnewP;

Using ".rawValue" wouldn't change the password variable at all, even in the same session.

I would be very grateful if anyone had an idea as to how to change the global variable using Javascript, which will hold when the file is saved and reopened.

".value" works within the session, but doesn't hold the value on reopenning.

I am attaching a sample form, with three signing buttons/fields.

Username1 = Niall O'Donovan     Password = nod

Username2 = Marianne Barry     Password = mb

I have initially set up the "Niall O'Donovan" account with .rawValue and the "Marianne Barry" account with .value scripting.

I am wondering if this is considered a security issue by Adobe and therefore we are restricted from changing global variables using Javascript???

Thanks,

Niall

14 Replies

Avatar

Level 10

Hi,

File is queued, so I have uploaded it onto Acrobat.com. It is available here https://share.acrobat.com/adc/document.do?docid=e65ec1c1-f5d2-40c1-88ea-ee6481f717c6

Thanks,

Niall

Avatar

Level 4

The Global Variables don't have the rawValue attribute, but you can access their value from de the value attribute (myGlobalVar.value).

Also, this variables behave just like a normal textField, this is, in order to make your changes to its value persistente, the PDF must be able to save itself into file (form fill-in).

Also note that what you are doing is a huge security risk, because if i open you PDF with Acrobat Pro or run it through Reader extensions and enable export data feature, i'll be able to read (clear text) all the global variables.

Avatar

Level 10

Thanks Rui,

I have been able to access the variables using ".value", as you suggested. However I have not been able to get the PDF XFA form to save the changes to the variables.

Can you elorabate on "the PDF must be able to save itself into file (form fill-in)."?

I am using LC Designer ES 8.2 and the users will be using Acrobat Professional and/or Acrobat Reader version 9. I have currently set the target version as 8.1. At the moment the form can be saved, but it does not appear to save the changes to the variables.

I appreciate the security risk (although I did not realise that variables were so accessible). The final form will be password protected (for opening in LC Designer) and the form is only intended for in-house use, it will not be going outside of the organisation.

Thanks,

Niall

Avatar

Level 4

Your Code works 99.9% you only have a typo in the Nial O'Donoval's name (in the names list of the signature JS and the global variable). Correcting that it changes the password.

What i wanted to say with the "the PDF must be able to save itself into file (form fill-in)." is that Dynamic PDF don't allow being saved with data (from data or global variables) by default, it has to be enabled,. You can either do it in Adobe Pro or using Livecycle Reader Extensions (both are payed solutions). If you do not, your code still works, but the changes won't be saved, so the next time the PDF is opened the password will be reseted to the default you puted in the pass[1-3] global field.

Avatar

Level 10

Hi Rui,

I enabled the form using Acrobat, but the global variables still would not hold on re-opening. I also tried saving the form as "static" (not enabled and enabled) and still no luck.

In the end I set up three textfields (hidden and with binding set to "none"), which capture the changes in passwords. Then I have preSave and Initialise events that push the textfield values back out to the global variables.

It works, but I would have prefered to keep all of this "stuff" off the form itself.

Thanks for all of your help on this!!!

Regards,

Niall

Avatar

Level 6

Hi Niall,

I know this is a very old post, but do you still have the final solution (actual document) to this?

What I'm trying to do is update a numeric field (used as an invoice #) each time a blank version of the doc opened.  I read somewhere that this is easily done in Acrobat via ( Advanced>Document>Processing>Document  Javascript), but I'm not quite sure how to implement in LC.

Thus far:

1. I've created a variable named "counter" under LC Form Properities. there is no value assigned, since I want the initial value to be zero "0"

2. On the form, I have a numeric field with the follwoing script on the "initialize" event:

          this.rawValue = counter.value + 1; //Pull counter valriable and increase by 1

3. Not sure if this next step is the correct, but I placed the following on the preSave event:

          counter.value = counter.value;

What I'm trying to do here in step #3 is update the counter variable (value) each time the document is saved. I also tried counter.value = this.rawValue; where "this.rawValue" would be the current value of the numeric field after it has been updated by the initialize event  (i.e. 01)

The issue I'm having is that although the form is updated to "01" upon initializing, it starts back at 0 when the form is reopened. I'm not sure if the counter.value is increasing at preSave (I know I can add the java debugger script to check), but whenever I close the form and open a blank copy it starts back at zero.  

Something tells me I'm missing something, although I did read in LC Help that: Form variable values reset each time you open a form.

1. So, then, how did you manage to keep user changed passwords "stored" in the form?

2. Also, when you save the form, are you doing a SaveAs, or simple Save?  When you start a new form, are you opening the previously saved (SaveAs'ed) version, or a blank version? If blank, how does the form know where the other version left off, if the documents aren't interconnected? To my knowledge, variables are stored "within" a form, and not a central location, so how count it even maintain an incrmental count in my situation?

Please advise.

Thanks

Shaun

Avatar

Level 10

Hi Shaun,

That is a bit of an old one

In my experiences Global Variables (in File > Form Properties > Variables) do not hold their amended .value in-between sessions. In addition the Global Variable is ONLY a text string type, hence you can't do numeric calculations on it. When you add a 1 to the Global Variable .value, it adds the 1 onto the end of the text string. This is why you are getting "01".

You can still use it as a counter. Your script would just need to look at the length of the string:

app.alert("My counter is " + counter.value.length);

Have a look at this example where I explore form variables: http://assure.ly/x1QObn.  It may help, as these are preserved in-between sessions. This is probably the best option.

The 2009 form did evolve and ended up something like this one: https://acrobat.com/#d=eFWndvuG-gt8GedvI-2erw. However I am not sure if it is using Global Variables (I haven't checked).

Good luck,

Niall

Avatar

Level 6

Hi Naill,

Yeah, 4 years is a long time (ha ha), but thanks for replying! Ha ha

FYI, I was using "01" as an example... however, since the text field (InvCounter) had a format pattern of " text{99}" it produced the "00" format I was looking for. When I added the script: "this.rawValue = counter.value + 1;" to the text field InvCounter  on the initialize event, I assumed it just added "1" to the variable (counter). I see what you're saying though re: the Global Vaiable is only a text string type.

Quick question...I glanced over the example you included.  Outside of using a LC server product, is it even feasible to create a "standalone" Global Variable that all forms can access? Or, is that perservation done within the "same" document all the time?

Even if I managed to get the form to preserve a gloabl variable within the same document, I'm not sure it would help in the case of an invoice, since users would be opening a blank version of the form each time they started a new invoice.

So, fr example, when he/she initially  creates a new invoice, it will start at 000 and will increase to 001 after initializing. When complete, he/she would then save the invoice - perhaps using SaveAs and giving it a new filename. When someone needs to create a new invoice, I thought it would be possible to open a new blank document and have start the InvCounter.rawValue set at 002. However, how is this possible if global variable are only preserved within a docuemnt's? Just wonder if there was a way to may place the variable "outside" the form so that new versions could access it.

Right now, it seems the only thing I can do is have the user open the "saved" copy, clear all fields except the Invoice # which was persevered and automatically updated upon opening document.  That could work, but it isn't practical since the user would always have to go back to the last saved copy.

Make sense?

Shaun

Avatar

Level 10

Hi Shaun,

I see where you are coming from now.

However I think this is going to be tricky to implement. You are correct that if you were using LiveCycle Enterprise Suite, you could set up a counter on the database, which would then keep track of the invoice numbers.

Outside of LC Enterprise Suite, you could set up a web service call or database call to a back-end counter, but this would not work if the user has Reader and the form is either NOT Reader Enabled OR only Reader Enabled in Acrobat. See summary here: http://assure.ly/etkFNU.

Anything other than a database, is going to get out of sequence very easy.

Maybe go for a manual approach for the invoice number, where the user checks up the next available number and manually inputs it.

Make sense?

Niall

Avatar

Level 6

Exactly! now you see what I was after, and my dilemma?

I thought about the manual approach, but that involves keeping track of the last number used, and there's potential for errors since the user could mistakenly duplicate an invoice number (if manually inserted), or if they forget to manually increment the invoice number (say via a screen-only button). I can make the form increase automatically, via the docReady event, but that puts me back having to open the last saved file in order to update the invoice - incrementally.

Another solution would be to use a unique invoice structure, whereby the invoice number is the Concatenation of perhaps the date (MMYY) and the user/company initials/abbreviation i.e. 0712ABC. That way I  don't have to worry about increments; although, there's still the possibility of duplicate numbers occurring (very slimmer).

I don't want to turn this mold into an even bigger mountain, so I think I'll go back and review some of your helpful samples and see what I can come up with.

Thank you for all your suggestions!

Shaun

Avatar

Level 10

I've done this on several projects using a unique identifier.

The following JavaScript code creates a 10 digit number based on the date and time. I have it check to see if there's a value in the field first (in case form has been used before) and only put in a new value if the field is empty.

I usually put it on the Initialize event of a text field (don't use a date field).

// check if field is empty and create number

if (this.isNull) {

   var d = new Date();

   this.rawValue = Math.floor(d / 1000);

}

// if field has a value leave it alone

else {

   this.rawValue = rawValue;

}

Avatar

Level 6

Jono,

Thanks for info you provided.  Are the generated invoice numbers coming out as 12 digits long on your end?

Avatar

Level 10

It gives me a 10 digit number.

Avatar

Level 2

Hi Niall

Sorry for digging an old thread.

could you please share the 2009 evolved file in dropbox or somewhere else other than acrobat. I am also trying to do similar time of form for my organisation.

thanks