Database Query to Find A Random Row

I am building a system which views shortcoded messages that can be sent to different contacts. I’m on the part that searches for a random contact to choose when seeing how your message will look before it is sent.

Hence I want to search my contacts table for a random contact, and I want that contact to generally be different each time the search is run.

Is there a simple way to do this…?

You can use the random filter bot on the server side and on the client side to show random record from a collection.
So if you want to do this on the clientside:

Select the query, then use the randomize formatter, which will randomize the collection

Then you need only one record from it, so add the Top formatter and add 1 as a value.

Then you need to get the value, so add the values formatter and select which column you want to show.

The result is:

On each page reload/server connect reload the data will be randomized.

Thanks for your detailed and prompt reply @teodor!

So would this be quick way to do it on the server side for a database query called m_contact?

{{m_contact.randomize()[0].id}}

No, you are missing the server connect name in your expression.
Should be something like:

{{serverconnect1.data.m_contact.randomize()[0].id}}
1 Like

I’m in a server action, not on the client side…

contact

Then it’s correct.

1 Like

Thanks @Teodor!