Skip to main content
Participant
September 9, 2017
Question

Generate number based on text from another field

  • September 9, 2017
  • 2 replies
  • 442 views

Hi all,

I'm trying to automatically generate a field based on 1) a person's name (from a drop-down menu, converted to a number); 2) the date; and 3) a random number. The resulting field would then take on the following (example) value: 10-20170101-1234, where "10" would be an employee's assigned number, "20170101" would be the date (as yyyymmdd), and "1234" would be the random number, with each segment separated by hyphens.

I have the second and third parts, but cannot figure out the JavaScript code to correctly populate the employees' assigned number. An example of what I've come up with so far is:

     var date = util.printd("yyyymmdd", new Date());

     var person = this.getField("Person").valueAsString;

         if (person == "Aaron") event.value = "1"; 

         else if (person == "Amanda") event.value = "2"; 

         else if (person == "Austin") event.value = "3"; 

     if (this.getField("Field").value == "")

     this.getField("Field").value = util.printf(person + "-" + date + "-" + "%04d", Math.floor((Math.random() * 10000) + 1));

The "date" and "random number" sections work, but I cannot get the first section to work correctly. If I add a "blank" option (e.g. "if (person== "") event.value = "";) the code will work, and return a blank value for the first segment, along with the correct date and a random number, but that's as far as I've gotten after searching through various forums.

Any thoughts would be much appreciated. Thanks!

This topic has been closed for replies.

2 replies

chrisa423Author
Participant
September 10, 2017

Okay, so I forgot that in the 'Options' for the drop-down menu you could assign an 'Export Value' for each Item, which is where I went wrong.

Using the following code, in conjunction with the respective "Export Values' from the 'Person' field should work. (Sorry, it may not be the 'cleanest' code ever.)

              var date = util.printd("yyyymmdd", new Date());

              var pnum = this.getField("Person").valueAsString;

                  if (pnum == "") event.value = "";  

              if (this.getField("Field").value == "")

              this.getField("Field").value = util.printf(pnum + "-" + date + "-" + "%03d", Math.floor((Math.random() * 1000) + 1));

Bernd Alheit
Community Expert
Community Expert
September 10, 2017

At which field do you use:

         var person = this.getField("Person").valueAsString;

         if (person == "Aaron") event.value = "1";

         else if (person == "Amanda") event.value = "2";

         else if (person == "Austin") event.value = "3";

chrisa423Author
Participant
September 10, 2017

Thanks for the reply. Your post made me rethink the relationship between the fields (e.g. to export the values for the respective 'person' codes from that field rather than trying to 'import' them via a script in the new field, so I finally got it to work. Thanks!