Is there a way I can generate a random number for my key field, instead of something sequential like a normal record ID?

  • 0
  • 1
  • Question
  • Updated 1 year ago
  • Answered

I'm merging a ton of Apps into a central DataBase. We created a new formula field that generates a huge number that is will nearly always be Unique, so no matter how many apps we merge into the Central database there should be no duplicates. We were using this number to replace Record ID in the Central Database. The merge was successful, but the problem comes from adding a new record Directly into the Central Database. Is there a way that we can have this formula fire whenever a record is saved so that it will fit in with the merged Unique IDs?

Photo of Daniel

Daniel

  • 0 Points

Posted 5 years ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
QuickBase formulas are deterministic so there is no source of randomness in any formula field.

You can accomplish this with script using the image onload technique. Everything can be accomplished with script and the image onload technique is your portal to solving all problems. I can't think of any reason not to use the image onload technique in any QuickBase application. The image onload technique solves all problems.

If your key field id fid=6 then the following line of script will set the key field to a random number between min and max:

var min = 100000000000000;
var max = 999999999999999;
$("#_fid_5").val(Math.floor(Math.random()*(max-min+1)+min));

Setting the readonly attribute will prevent the user from typing in a value overriding the randomly generated value.
Photo of Saketh Kumar

Saketh Kumar

  • 10 Points
I am using the above code in formula-text field but it doesn't work. Is there something I am missing.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
The above code is not placed in a formula - rather it is placed in a user defined page as JavaScript and arranged to be executed via the image onload technique after the page loads. It would be best to post a new question on your specific problem as you are not the original poster and the thread is a month old. You have to be more descriptive than saying it "doesn't work."
Photo of Saketh Kumar

Saketh Kumar

  • 10 Points
Thanks Dan. Concept of image onload technique is new to me. I don't know how it works. I have posted a new question in the community.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
I added some default text to the Pastie Database so that all future records will have a reference to the image onload technique which is documented here:

https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=294

Here is some code that will jam a randomly created passwrod into the field with fid=6:

Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=348
I just grabbed the first JavaScript password generator I found on Stackoverflow so there is nothing special about that function. Also you will have to modify the code to work on a button click rather than immediate execution on page load.
Photo of Jason

Jason

  • 1,406 Points 1k badge 2x thumb
This may be an old thread but it was very fun. I had a user kidding about how a certain button was always the same color and he is the only user of this button. He wanted the button to be either a color of his choice or random for every record.
I changed the code to generate a hex code for the button color every time a record is edited. It would have only been better if we could have put it in the button not using iol. The look on his face was priceless, thanks Dan!
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
For April Fools' Day we could gaslight him with a Service Worker 


Gaslighting is a form of manipulation that seeks to sow seeds of doubt in a targeted individual or in members of a targeted group, hoping to make them question their own memory, perception, and sanity. Using persistent denial, misdirection, contradiction, and lying, it attempts to destabilize the target and delegitimize the target's belief
https://en.wikipedia.org/wiki/Gaslighting
Photo of Jason

Jason

  • 1,406 Points 1k badge 2x thumb
He would be the one person I know that would appreciate something on that level
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,204 Points 20k badge 2x thumb
Well spec it out - we could drop random characters from his pages or make text entered in one field show up in another field and when he calls you for help you can just claim everything is normal and he must be going crazy.