Skip to main content
Participant
January 18, 2024
Question

Sequential Invoice number.

  • January 18, 2024
  • 1 reply
  • 1206 views

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.

 

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
January 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
January 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.

try67
Community Expert
Community Expert
January 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.