Forum Discussion

DanielDaniel3's avatar
DanielDaniel3
Qrew Member
10 years ago

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

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?

9 Replies

  • 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.
  • I am using the above code in formula-text field but it doesn't work. Is there something I am missing.
  • 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."
  • 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.
  • 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.
  • JasonJohnson's avatar
    JasonJohnson
    Qrew Assistant Captain
    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!
  • 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
  • JasonJohnson's avatar
    JasonJohnson
    Qrew Assistant Captain
    He would be the one person I know that would appreciate something on that level
    • _anomDiebolt_'s avatar
      _anomDiebolt_
      Qrew Elite
      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.