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

Sequential Invoice number.

Community Beginner ,
Jan 18, 2024 Jan 18, 2024

I am trying to create a Invoice with a invoice number which rolls to the next number automatically.  I have it currently doing this but when I save as the saved as version get a new number and the original template keeps the old number.  I am looking for this to be reversed but can't seem to figure out how.  I am not good with Java Script or Adobe my kids would call me a "noob".  I am trying to figure this out and have read some of the post on here but can't seem to get it to work the way I need it.  I am using Adobe Acrobat.  Thanks in advance for all help and suggestions.

 

TOPICS
How to , PDF forms
1.3K
Translate
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 ,
Jan 18, 2024 Jan 18, 2024

Creating unique consecutive numbers is tricky business for a document script. 

Since the script is in the document, every copy of the document acts independently. There is no way to track or control serial numbers if it is all handled internally to the document.

I'd suggest using the global object to save the last serial number. That way the next number is independent of the copy of the document. But it is dependent on your install of Acrobat.    

  

 

 

 

 

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

Translate
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 ,
Jan 18, 2024 Jan 18, 2024

If I used the Script below would the randomly generated number ever repeat its self?  Can't have two invoices with the same number? 

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

 

Also is there a way to make the script only assigned to the Template?  Or is there a way when saving to make all fillable and other areas locked?  Just brain storming here trying to figure out how to stop the number from generating everytime it is printed or saved or opened.  I am sooo close to having this document working I think this is one of the last things.

Translate
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 ,
Jan 18, 2024 Jan 18, 2024

Theoretically, yes, it's possible to get a duplicate value. The chances of it are [Number of invoices you generated before]/1000000, in this instance. If you really want to minimize that chance use a time stamp, like this:

this.getField("Load#").value = new Date().getTime();

This will ensure the value is unique, unless two people manage to do it at the exact same millisecond, which is extremely unlikely.

Translate
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 ,
Jan 19, 2024 Jan 19, 2024

is this in conjunction with the script from above?  or stand alone

 

Translate
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 ,
Jan 18, 2024 Jan 18, 2024

To get a unique number I would suggest using the SOAP.streamDigest() function to create a hash of the current timestamp.

This technique is pretty reliable. 

 

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#streamdigest

 

 

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

Translate
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 ,
Jan 19, 2024 Jan 19, 2024

I looked but not sure I understand this or how to put it in the form

Translate
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 ,
Jan 19, 2024 Jan 19, 2024
LATEST

 

Here's an example

this.getField("Load#").value = SOAP.streamDigest(util.streamFromString(util.printd("yyyymmddTHHMMss",new Date())),"SHA1").read();

 

There are different ways to do this. This code returns a Hex encoded string.  If you wanted a different character set, for example all numbers you could convert it to decimal numbers. 

 

 

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

Translate
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