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

Level 9

No errors in console. I will recheck the script. Will take a while.

Avatar

Level 9

Is it possible to tie this script to an object - like when a radio button is chosen and have it only run once even if they change their choice?

Avatar

Level 9

I tried running the script when entering a text field. Works okay - but then I exit the textfield and re-enter and the form number field goes to null (blank) - why?

  This is the script I am using...

if (Company.ARnumber.rawValue == null) {

          var d = new Date();

          //divide by 1000 to get seconds and trim decimals

          Company.ARnumber.rawValue = Math.floor(d / 1000);

} else {Company.ARnumber.rawValue = this.rawValue;

}

Avatar

Level 10

Yeah you can put it on a button or the like. You just need to change the references to "this" to the name of the field.

if (fieldName.rawValue == null) {

          var d = new Date();

          //divide by 1000 to get seconds and trim decimals

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

} else {

          fieldName.rawValue = fieldName.rawValue;

}

Avatar

Level 10

You need to change the last "this" reference to the name of the field.

Avatar

Level 9

I found the problem - in the last line of script I did not change "this" to the name of the form number field. I am sorry to have caused you all this trouble.

Avatar

Level 2

CAN I MAKE A BUTTON THAT ABLE TO REFRESH THAT CODE IN THE FIELD

SO I CAN GENERATE A NEW NUMBER EVERY TIME I PRESS THAT BUTTON ?

Avatar

Level 10

Hi Abduzer, you would just put the code on the Click event of a button.

And if you always want a new number and don't care if the field already has a value then you just need:


var d = new Date();

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


Avatar

Level 1

This is a great script.  I am trying to trim off 4 digits from the left side and having no luck. Would you be able to help me? Thanks!

Avatar

Level 10

Give this a shot. I was getting errors with substring() until I realized it wasn't getting a string.

var d = new Date();

var t = Math.floor(d/1000).toString();

TextField1.rawValue = t.substring(4);

Avatar

Level 1

THANKS! It works perfectly! You are awesome!

Avatar

Level 1

First. I know NOTHING about javascript.

I've been able to limp along following your instructions... and THANK YOU.

But... I cannot get the "Invoice Number" to save and send when attaching it to email.

What am I doing wrong?

Avatar

Level 1

Please help - I can not figure this out.  I need a field at the top of my form to generate a unique number to identify the request.

I would like my form to generate the field based on the date of the request, but I might have more than one request a day... so for example a request generated in May of 2017 would be 2017-05-#### (the ### being the unique numbers)

How do I do this?!?!?

Avatar

Level 9

Without connecting your form to a database, the form has no way of knowing how many have been created in a particular day. Therefore, the only suggestion I can give you is to use today's date followed by the time formula in this post. The time formula is ten digits long and is a rounded off number but always unique.

Good luck!

Avatar

Level 1

Hi Don,

Thanks but it still doesn't seem to be working for me.  This is what I have in the script box:

form1.#subform[0].Request::initialize - (FormCalc, server)

var d = new Date():

request.rawValue = Math.floor(d/1000).toString();

request.rawValue = t.substring(4);

what am I doing wrong?  When I preview the PDF - the Request # field is still blank...

Avatar

Level 9

In JavaScript:

if (Company.ARnumber.rawValue == null) {

          var d = new Date();

          //divide by 1000 to get seconds and trim decimals

          Company.ARnumber.rawValue = "AR" + Math.floor(d / 1000)

         

} else {Company.ARnumber.rawValue = Company.ARnumber.rawValue;

}

Avatar

Level 1

Great I got it to work - thank you.  How would I connect it to a database to generate sequential numbers based on the year?

so the form's unique ID would be "IR-2017-001" and then "IR-2017-002" when the first form was printed and saved

thank you for all your help!!  I don't get scripting at all!!!