Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
is this in conjunction with the script from above? or stand alone
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I looked but not sure I understand this or how to put it in the form
Copy link to clipboard
Copied
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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more