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

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

New Here ,
Jun 07, 2024 Jun 07, 2024

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));

TOPICS
How to , JavaScript , PDF forms
1.7K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Jun 07, 2024 Jun 07, 2024

Search JavaScript in the tools search then click Document JavaScripts, then name the script and copy and paste the script.  When you close the window the field will advance by 1.  Clear the field and save the PDF.  The number will advance every time you open it.

View solution in original post

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 ,
Jun 07, 2024 Jun 07, 2024

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

View solution in original post

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 ,
Jun 07, 2024 Jun 07, 2024

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

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
New Here ,
Jun 07, 2024 Jun 07, 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.  

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 ,
Jun 07, 2024 Jun 07, 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.

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
New Here ,
Jun 07, 2024 Jun 07, 2024

I have it listed under Customer Calculation Script under the Calculate tab.  As well as Run Custom Validation Script under the validate tab.  In the Actions tab I have Mouse Enter, Run a JavaScript.  

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 ,
Jun 07, 2024 Jun 07, 2024

Remove all of those scripts and enter it where I said.

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
New Here ,
Jun 07, 2024 Jun 07, 2024

Sorry I don't see where you said to remove and add.  I see a post saying "You don't need the util.printf script if you format the field as a number with no seperator and no decimal places."  Can you please clarify?  Thanks

 

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
New Here ,
Jun 07, 2024 Jun 07, 2024

Sorry I see it now.  

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
New Here ,
Jun 07, 2024 Jun 07, 2024

Sorry, where does the script go, i'm not sure where the document level script goes.  Can you please clarify?  

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 ,
Jun 07, 2024 Jun 07, 2024

Search JavaScript in the tools search then click Document JavaScripts, then name the script and copy and paste the script.  When you close the window the field will advance by 1.  Clear the field and save the PDF.  The number will advance every time you open it.

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
New Here ,
Jun 07, 2024 Jun 07, 2024

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?

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 ,
Jun 07, 2024 Jun 07, 2024

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

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
New Here ,
Feb 05, 2025 Feb 05, 2025

Hi! I used this code for my document as well and it works on my computer, but I need it to work on mobile devices as well. I'm running into an issue where this code isnt running itself when used on phones either in the abode app or in a browser. 

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 ,
Feb 05, 2025 Feb 05, 2025
LATEST

JavaScripts supported by mobile PDF viewers are very limited.

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