1. Knowledge Base
  2. How-To Guides
  3. Logic, Equations, & Formulas

How to Set a Random ID

In this article, we will explore the process of utilizing a text formula field to generate a random ID.

Use Case

If you require a random ID, such as for new user sign-ups or generating coupon codes, this guide will assist you in achieving that goal.

Since Text Formulas and Equations recalculate every time a record is updated, the random function: random() will set a different value each time.

The final step in these instructions enables the storage of this value, transforming the initial random number into a fixed ID.

 

Requirements

If this is your first time creating an app, you'll need to know some basics about adding tables, fields, pages, and views. You can start by reading our Builder Basics section. 
Other good resources can be found in our About Your Database and Working With Pages article in our knowledge base.

 

Steps

1.  Adding a New Text Formula Field

In the Data section of the Builder, you can add a text formula field to a table. Choose a name for the field, such as "Random ID".

Note: Avoid utilizing the "Random ID" field in any other part of your application as it will change with every update of a record.

Input the following text into the Equation editor: 

randomid1

Tip:  To learn more about available text formula functions, see our article here

 

 

2.  Store Value: Adding a Conditional Rule to a Short Text Field

If you wish to retain the initial random number generated, you can proceed with the following steps.

  1. On the same table used for the step above, add a new short text field named "ID".

  2. Select to edit the ID short text field.

  3. Select on the "Conditional Rules" tab.

  4. Check the checkbox for "Add conditional rules."

  5. Add a rule:

    • When:  "ID" > "is blank"

    • Set:  "to a field value" > "Random ID" 

randomID2

In this case, the ID field will be set only once. Once the ID field value is no longer blank, it will no longer update.

Note: The short text field that you create must be placed below the text formula field in order to be populated correctly. This is due to the order of operations when calculating equations or formulas.

 
That's it! You have successfully implemented a solution to generate a random ID.