Skip to main content
bc2018
Participant
December 7, 2018
Question

Generating a random number.

  • December 7, 2018
  • 2 replies
  • 2005 views

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!

This topic has been closed for replies.

2 replies

bc2018
bc2018Author
Participant
December 7, 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.

try67
Community Expert
Community Expert
December 7, 2018

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

try67
Community Expert
Community Expert
December 7, 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.

try67
Community Expert
Community Expert
December 7, 2018

Sorry, 0.01%...