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

Sequential numbering script in form field

New Here ,
Apr 26, 2023 Apr 26, 2023

I am trying to create a senario where everytime a user opens a master form, a number is generated sequentially in the REPORT NUMBER field.

I tried to run the script pictured below, but it's not what I need.

Ashley250449787imy_2-1682534682092.pngexpand image

 

  • The only number that needs to change in the field is the number just before the -023.
  • The user intends to track of this report in this format: 016-23 translating to the 16th report of 2023.
  • If possible, I'd like for the report number to change each time the user opens the form.

 

Brainstorming:

  • User opens master document
  • The generated number appears in the "REPORT NUMBER" field e.g. 016-023
  • The user completes the form then saves a copy on computer under "NDT-016-023".
  • When the document "NDT-016-023" is reopened, I would like the REPORT NUMBER to change to 017-023.

 

Can anyone advise a possible solution for this?

 

Thank you!

TOPICS
General troubleshooting , How to , JavaScript , PDF forms
1.5K
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
1 ACCEPTED SOLUTION
Community Expert ,
Apr 26, 2023 Apr 26, 2023
LATEST

Looks like you copied this from an earlier post on exactly the same topic. All you need to do is change the code to match the format you want. Part of this is using actual Acrobat JavaScript functions and not making things up. 

 

Here's a simple redo:

var oRptNumFld = this.getField("Text1");
// Divide the number into it's two parts
var aRptNumParts = oRptNumFld.valueAsString.split("-");
aRptNumParts[0] = (Number(aRptNumParts[0])+1).toString();
oRptNumFld.value = aRptNumParts.join("-");

 

However, this approach is problematic. As soon as a copy of the form is made the numbers will  change out of sequence. Also anyone who opens the copy will see a different number than the one that it was saved with. And if they save the form, then the next person will see a different number. It's important to think these things through.  

One solution would be to save two copied. One that is the working original form, and the other a flattened form for distribution. 

Another solution would be to save the increment number in a global variable. So the increment can only happen on the origial system. If the form is opened on any other computer, then no increment will take place. 

And yet another solution is to not put any auto-increment code in the form at all. But to use an automation script to handle all of this. The same automation script could also save the form to the correct name. 

 

There are many other ways to do this. All of them are better than putting the auto-increment code on the document. 

 

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

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 ,
Apr 26, 2023 Apr 26, 2023
LATEST

Looks like you copied this from an earlier post on exactly the same topic. All you need to do is change the code to match the format you want. Part of this is using actual Acrobat JavaScript functions and not making things up. 

 

Here's a simple redo:

var oRptNumFld = this.getField("Text1");
// Divide the number into it's two parts
var aRptNumParts = oRptNumFld.valueAsString.split("-");
aRptNumParts[0] = (Number(aRptNumParts[0])+1).toString();
oRptNumFld.value = aRptNumParts.join("-");

 

However, this approach is problematic. As soon as a copy of the form is made the numbers will  change out of sequence. Also anyone who opens the copy will see a different number than the one that it was saved with. And if they save the form, then the next person will see a different number. It's important to think these things through.  

One solution would be to save two copied. One that is the working original form, and the other a flattened form for distribution. 

Another solution would be to save the increment number in a global variable. So the increment can only happen on the origial system. If the form is opened on any other computer, then no increment will take place. 

And yet another solution is to not put any auto-increment code in the form at all. But to use an automation script to handle all of this. The same automation script could also save the form to the correct name. 

 

There are many other ways to do this. All of them are better than putting the auto-increment code on the document. 

 

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