You would like assign a random ID to new user sign-ups or generate coupon codes.
Since Text Formulas and Equations recalculate every time a record is updated, the random function ["random()"] will set a different value each time.
The last step in these instructions allow for this value to be stored, so the first random number generated becomes a static ID.
If you are creating your first app, you'll need to know some basics about adding objects and fields. You can start by watching our "Introducing Knack" video tour and then working through the "Data: About Your Database" article.
Add a new text formula field
- Add a text formula field called "Random ID" to an object. We'll use the Contact object for this example.
Set text formula field to value
- Click to edit the Random ID short text field.
- Click in the formula field and enter the random() function, with any number of characters that you want your ID to have, eg. random(7).
Store Value: Add a conditional rule to short text field
If you'd like to store the first random number generated, you can continue with the following steps.
- On the same Contact object, add a new short text field called ID.
- Click to edit the ID short text field.
- Click on the "Conditional Rules" tab, and then checkbox "Add conditional rules".
- Add a rule:
- When "ID" "is blank",
- Value Set "to a record value" "Random ID".
This way, it will only be set once; once ID is no longer blank, it will not reset.
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.
Notes & Troubleshooting
Do not use the Random ID field elsewhere in your app, since it will change with each record update.