Finding the number of records in a table / getting a random record


#1

Hi,

I want to be able to pull a record at random from my table via the js api.

To do so I need to know the max number of records that exist in the table. I have used Auto numbering to create the id ( key field ) of the table. I can then simply generate a rand number and pull the record that has that rand number as it’s id.

How ever I am stuck at present trying to figure out where I can get the number of records from, is there away to query the size of the table? or retrive the last record ( because of the auto numbering it’s ID would be the max record number )

thank you for your help and time.


#2

Hi Jennie, if you want to get the last auto number you would just do a query and sort by that auto number descending and set maxLimit to 1 to retrieve the highest auto number.

However, the issue you’ll run into is if you ever delete any records then the auto number will no longer represent how many records exist.

Another approach is to have another table with just 1 record and then on your first table have a link to this table where every record has a link to that 1 record, then add a Count field to that table to show how many are referring to that 1 record. So long as every record in the first table points to it it will be an accurate reflection of the count.

Good luck!