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

Generating a random number.

New Here ,
Dec 07, 2018 Dec 07, 2018

I can not seem to figure out how I can insert a random number generator into a work order form that I have already created and put all other required field items into. I am not savy when it comes to any of the JavaScript stuff. I need the number to be (4) digits and I need it to re-generate on each new form after it is filled out and saved or upon opening a new one. any step by step help would be greatly appreciated!

TOPICS
PDF forms
2.0K
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 ,
Dec 07, 2018 Dec 07, 2018

You want it to change the number after the form is saved? That will just mean it will be lost, unless the user re-saves the form (which will cause a new number to be generated, etc.). I think the best is to do so before the file is sent.


To do so you can use the following code (let's say you want to place it in a text field called "Unique ID"):

this.getField("Unique ID").value = Math.random().toString().substr(2,4);

Keep in mind you could get duplicates, though. There's a 0.1% chance of getting a duplicate number the second time you generate it, and the more you use it, the higher that number becomes.

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 ,
Dec 07, 2018 Dec 07, 2018

Sorry, 0.01%...

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 ,
Dec 07, 2018 Dec 07, 2018

sorry for the wording Try67. what I meant was id want it to generate a new number each time it is opened (as a blank) work order form. im confused on where exactly to input any of these codes.

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 ,
Dec 07, 2018 Dec 07, 2018
LATEST

So you want to only populate the field if it's empty? The go to Tools - JavaScript - Document JavaScripts and create a new item (let's call it "scripts"), with the following code (delete the code that is generated by default when you open the editor window):

if (this.getField("Unique ID").valueAsString=="")

     this.getField("Unique ID").value = Math.random().toString().substr(2,4);

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