Skip to main content
Joseph_Alvarado
Participating Frequently
June 7, 2024
Answered

JavaScript - Set file number counter everytime a file is opened and save with that.

  • June 7, 2024
  • 1 reply
  • 2461 views

Hello,

 

I am in the process of creating a contract template for our company.  We would like to be able to send this document out to our customers for them to sign.  I would like to assign a different contract number to each document that I resave (save as) under a customer's name.  Everytime I do this I would like to have the contract start with a new distinct contract number.  I will be working off of the master template (file).  I would like the number to start with 100001.  Currently, I am using this JavaScript but it is only putting one digit at a time.  I started with a 1, then number 2, now I am at number 3.  Only one single digit.  My field is titled Agreement Number.  Can you please help me with a solution?  

 

var f = this.getField("Agreement Number");
if (f.value=="") f.value = "100001";
else f.value = util.printf("%04d", (Number(f.value)+1));

Correct answer PDF Automation Station

I followed these steps but it still doesn't want to update when I save and then create another.  I close the file and only 100001 appears in the field.  Do I need to leave the parenthesis in the script when referring to the file name and the field name?


Copy and paste the script exactly.  Then change "Template.pdf" to whatever your file name is and include the .pdf file extension.

1 reply

PDF Automation Station
Community Expert
Community Expert
June 7, 2024

Your script advances the number by 1.  What would you like it to do?

Joseph_Alvarado
Participating Frequently
June 7, 2024

I want it to display 100001 on the document and so forth. (100002)  But I want it to show the next number in the sequence each time I save a copy and rename it.  

PDF Automation Station
Community Expert
Community Expert
June 7, 2024

Where is your script located?  You should enter it as a document level script with a test to make sure the file name is "Template.pdf" - or whatever you're calling it.  That way, once the file is saved under a different name, the script won't advance the number when the new contract is re-opened.  Also, upon opening the template you should immediately save it so the new number is saved in it.  Every time you open the template the number will advance by 1 (save the template, then "Save As" with a different name).  Here's the document level script (than runs every time the doc is opened):

if(this.documentFileName=="Template.pdf")
{
var f = this.getField("Agreement Number");
if (f.value=="") f.value = "100001";
else f.value++;
}

 

You don't need the util.printf script if you format the field as a number with no seperator and no decimal places.