Retrieve highest value of all records

For this you should use a sequence. The sequence will generate the next number.

An example can be found in Party module, where if a checkbox in Party configuration is checked, then a new party will get it’s code from the configured sequence.