• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
4

How to get PDF to auto generate new invoice number everytime it is open

Guest
Jan 09, 2015 Jan 09, 2015

Copy link to clipboard

Copied

I need a unique invoice number to show in the corner of a form.  I was thinking of the best way (maybe the easiest?) to generate a number..  I want the form when opened to automatically create this number.  In addition, one the PDF has been filled out and so on, then saved, I dont want that invoice number to regenerate once its opened again for whatever the reason.  Does this sound possible?

TOPICS
PDF forms

Views

75.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jan 12, 2015 Jan 12, 2015

Copy link to clipboard

Copied

My bad... The code should be:

this.getField("Invoice No").value = util.printf("%06d", Math.floor((Math.random() * 1000000) + 1));

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
replies 139 Replies 139
Community Beginner ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

Hi TRY67,

I am trying to generating automatic numbering for a file each time it opens starting from 000501 then getting it to save the form after completion on exit. I enterred the code below:

if (this.documentFileName == "TFMC Work Order Form.pdf")

if (global.count == null) //checks to see if count exists

{

global.count = 000501 //sets initial count, you can change to suite your needs

global.setPersistent("count",true) //makes the counter a global object so it is persistent data

}

else

global.count++ //increments the the counter by one when the document is opened

var f = this.getField("Order Number").value = Number(this.getField("Order Number").valueAsString)+1; //creates a variable for the form field where the counter will be displayed

f.value = global.count //sets the form field to equal the counter

but it doesn't generate the number even if I start the field area with 501, howver if I go into prepare form the number sometimes runs a counter in the field 4, or 5. Auto generating only when I go into prepare form  not always, only sometimes. 

Can you assist?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 14, 2023 Aug 14, 2023

Copy link to clipboard

Copied

There are several issues with the script. 

First, the global object is local. The form will start over at 501 for every different person who opens it.

Next, the variable "f" is set to the field value of the "Order Number" field. The code "f.value" does nothing.

Replace the last two lines with 

 

this.getField("Order Number").value = global.count;

 

There are other problems, but this will get you started.

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

Thanks again! Do I need to input anything in the brackets at new Date?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

No, no parameters required.

On Tue, Feb 28, 2017 at 11:58 PM, chrism53001470 <forums_noreply@adobe.com>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

Great! One final question before I leave you alone, will this definitely stay unique or will it repeat in the future some time?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

It will stay unique. It's basically a counter of the number of milliseconds that have passed from a specific date known as the epoch (01/01/1970), so there's no chance of it repeating itself, it only gets larger as time passes.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

Hi Again mate,

Unfortunately I've just been informed that the unique code needs to be 5 digits - no more, no less.

Aside from using     this.getField("Invoice No").value = util.printf("%06d", Math.floor((Math.random() * 99999) + 10000)); 

Do you know if there is a more unique way of doing this without the possibility of repeated numbers?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

No, that's the only way. And there is no way to ensure that you won't get duplicates, unless you assign all the numbers in a single location and maintain a list of those that were already used.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

Sorry to ask you another question, but I'm using

if (this.getField("Form No").valueAsString=="") { 

    this.getField("Form No").value = util.printf("%06d", Math.floor((Math.random() * 9999) + 1000)); 

}

and trying variations of the * 9999) + 1000) numbers, but it seems to have a minimum of 6 digits (with the first digits being zeros) no matter what combination I use. Do you know the correct numbers to make sure it generates a 5 digit number (no more, no less)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

Actually I worked it out I think.

Changed the ("%06d", to ("%05d", and it seems to have worked. Using a range of *99999) + 10000));

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2017 Feb 28, 2017

Copy link to clipboard

Copied

No luck,

    if (this.getField("Form No").valueAsString=="") { 

    this.getField("Form No").value = util.printf("%05d", Math.floor((Math.random() * 99999) + 10000)); 

}

The number generated ranges from 5-6 digits!

What am I doing wrong?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2017 Mar 01, 2017

Copy link to clipboard

Copied

Why are you adding 10000 to it? You only need to add 1, to make sure it's not zero...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 07, 2017 Jun 07, 2017

Copy link to clipboard

Copied

Hi try67,

how to get a new invoice number every time after printing the document?

Thx.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 07, 2017 Jun 07, 2017

Copy link to clipboard

Copied

Move the code given above to the Did Print event of the document (under JavaScript - Set Document Actions).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 07, 2017 Jun 07, 2017

Copy link to clipboard

Copied

try67, you are the KING

Thanks a lot.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

hy. i use this code to generate the invoice number, but after i put the script the filed is filled with the random number, and if i save the file then when i opened the number remain the same. so how i can make the file like an template with that filed empty - to have an master file, and open and only then the scipt to do his job?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 27, 2022 Sep 27, 2022

Copy link to clipboard

Copied

I haven't looked through all the posts and code in this thread, and I don't know how your are doing it. However, it is common to setup of the "unique number script" as a document level script that seta the field value when it is empty.  If this is the case, then simply save the document with an empty ID field.   Then, whenever it is opened it will automatically fill with a random ID value. Then save the file with a different name. 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 06, 2017 Sep 06, 2017

Copy link to clipboard

Copied

This is great!

I have an additional question:

My file is titled "Complaint Form": the unique ID is great for cataloging these. I am wondering if there is a way to have the ID number attach to the file name by default when the file is saved.

I.e. If I open the file to document a complaint and want to save it after the conversation, but have the Unique ID and file name perpetually retain.

     1. Open file "Complaint Form"

     2. Let's say the ID that is generated is "00046587"

     3. I fill in the fields and then select File → "Save As"

     4. Can it be programmed to recognize this instance as "Complaint Form - 00046587" or even "00046587 - Complaint Form" so      that they are more easily arranged in my file folder.

Any help would be much appreciated

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 19, 2018 Jun 19, 2018

Copy link to clipboard

Copied

Hello! I used this script to randomly generate an invoice # on my pdf, but when I test it and email a saved form to myself and reopen the document, the number regenerates into something new. Is there a way to have the number stay the same as soon as it the document is saved. This document will be placed via link on a website and people will open it, fill it out, save it and then email it to me and the number they save it as needs to be the same when it gets to me or any time after that when I open it.

I was also wondering if it was possible to have an invoice # generate in the form yymmddhhmm-rr where the digits are year year, month month, day day, hour hour, minute minute - random random. with the same saving requirements as listed above.

Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 19, 2018 Jun 19, 2018

Copy link to clipboard

Copied

Yes, it's possible, but as I wrote, you need to clearly define when it should change and when it shouldn't.

For example, a script can be applied to the Submit button that sets the value of a hidden check-box, and then the script that populates the text field will look at that check-box and will only edit the invoice number if it's not ticked, or something like that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 19, 2018 Jun 19, 2018

Copy link to clipboard

Copied

Sorry-this is my first time really trying this out. So I made the hidden check box, but I don't know how to make it so when somebody hits the submit button, the check box is checked and I also don't know how the script would then look to this box to ensure that the invoice number doesn't change now.

I used your code for the new invoice generator and it works great! Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 19, 2018 Jun 19, 2018

Copy link to clipboard

Copied

Before the Submit action add this:

this.getField("Checkbox1").checkThisBox(0, true);

And before the code that changes the value of the text field:

if (this.getField("Checkbox1").valueAsString=="Off")

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 19, 2018 Jun 19, 2018

Copy link to clipboard

Copied

I'm getting an error when I insert the second code. I'm inserting it into the Document JavaScript under the JavaScript editor mode. Should I be inserting it in the All JavaScripts window?

error.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 19, 2018 Jun 19, 2018

Copy link to clipboard

Copied

You need to add the command that changes the value of the field after it, as a part of the same item...

Do NOT use the Edit All JavaScripts command to edit the code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2018 Jun 20, 2018

Copy link to clipboard

Copied

The check mark is working fine with the submit button, but when I save and reopen the document, it still regenerates the invoice number. I have the code to check the check box listed directly ahead of the number generation code in the javascript window. Am I missing something?Capture.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines