Expand my Community achievements bar.

SOLVED

Form Automatic Numbering

Avatar

Level 9

Is it possible with a LiveCycle form that is not connected to a database, to automatically have a unique sequential form number populate a textfield when the form is opened? This would be the number that identifies that version of the form.

1 Accepted Solution

Avatar

Correct answer by
Level 10

I've used this script for a bunch of forms.

It uses the date to generate a mostly unique number — forms would have to be opened at exactly the same time to get a collision.

Put it on the Initialize event of a text field. It also checks to see if the field already has a value and if it does it leaves the value as-is, so if someone saves the form it keeps the same number. You can remove the else statement if you're not worried about that.

if (this.rawValue == null) {

          var d = new Date();

          //divide by 1000 to get seconds and trim decimals

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

} else {

          this.rawValue = this.rawValue;

}

View solution in original post

38 Replies

Avatar

Correct answer by
Level 10

I've used this script for a bunch of forms.

It uses the date to generate a mostly unique number — forms would have to be opened at exactly the same time to get a collision.

Put it on the Initialize event of a text field. It also checks to see if the field already has a value and if it does it leaves the value as-is, so if someone saves the form it keeps the same number. You can remove the else statement if you're not worried about that.

if (this.rawValue == null) {

          var d = new Date();

          //divide by 1000 to get seconds and trim decimals

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

} else {

          this.rawValue = this.rawValue;

}

Avatar

Level 9

Thank you - I will give it a try and report back.

Avatar

Level 10

Actually you can remove the whole if/else statement if you don't want the check - you just need lines 2 and 4 of that script.

Avatar

Level 9

That certainly works but it is a very long number and may be diffcult to use in day-to-day business practice. Thank you for your reply.

~Don

Avatar

Level 10

It's ten digits, same as a phone number with area code.

I guess it depends on how you intend on using it. I went this route because a real UUID (universally unique identifier) is far worse if one needed to enter it by hand (36 characters, letters and numbers).

You could divide by a larger number to make the number shorter but then your chance of a collision goes up. Or perhaps you could trim numbers from the left side, effectively retaining the minutes, seconds or milliseconds but getting rid of the years, etc. (the Date() number is the number of milliseconds since the epoch which is January 1, 1970, for JavaScript).

Avatar

Level 9

We have decided to go with your script because we agree with what you just stated. I appreciate your response and expertise!

~Don

Avatar

Level 9

Does this work when the LiveCycle form has Reader Extended Features enabled?

Avatar

Level 10

Yes, I've used it on several extended forms.

Avatar

Level 9

For some reason, mine stopped working when I enabled those features. Any ideas why?

Avatar

Level 9

I just tried turning off the extended features and it started working again. Is there anything I can do to fix this?

Avatar

Level 10

It should work fine...

Oh, are you using the check to see if the field has a value already? In which case you'll have saved the form with a value in the field so it won't change.

What you have to do is enter a line of code in the Acrobat console that will null the field before saving the file with reader extensions.

You need the full path to the field in Designer starting at the top node in the hierarchy (probably form1). So on one of my forms I have the field on the Master Page and the path is form1.Page1.formUUID (ignore the Master Pages node if using a Master Page).

Open the form in Acrobat and then open the JavaScript console (ctrl-J). In the console you need to type:
this.xfa.form.(path to field).rawValue = null;

In my case above it's this.xfa.form.form1.Page1.formUUID.rawValue = null;

Extend and save the pdf once the field is blank.

Avatar

Level 9

Yes, I copied the code you originally suggested because I don't want the form number to change once it is populated. This form will be opened, top section completed, then emailed to the next person who completes the remainder of the form. When the first person opens the blank form, I want it to populate the form number field and then keep that number when the form is attached to an email (button) and sent to the second person.

Will what you just suggested allow that process?

Thanks for all your help!

Avatar

Level 10

Yup, you just have to make sure you extend and save the file after blanking the field and then use that file as the one you send out.

Once the pdf is saved with the field blank the script will fill the field.

Avatar

Level 9

This is what I have but the form number is still in the field. Do I push "enter" after entering this script or have I got the script wrong? (To confirm the correct path I used Crtl + Shift and clicked the field)

AcrobatWindow.png

Avatar

Level 10

Oh, sorry, forgot that bit; you have to press ctrl-enter or use the enter key on the numberpad.

Avatar

Level 10

I just had to do a check to test, but I think it is because you've got dashes in your field names. Best to stay away from them.

Also, did you rename form1 to FRM-MEG-02-NoRev at the top of the hierarchy? You need form1 in there unless you've renamed it.

Avatar

Level 9

Yup - that was the problem. I removed the dashes and now, thanks to all your help, know to stay away from them! Thank you so much!

~Don

Avatar

Level 9

Maybe it's me but...

When I save the Reader extended form that has a form number in the form field, and then reopen the form, the number changes! Why? Do I have to somehow shut off the Acrobat script?

Avatar

Level 10

No, that script checks for a value and leaves it alone if it finds one. Are you getting any errors in the console?

I've used it many times and it always works fine.