How can one generate a unique numbers that does ensure consistent records

I have a sample registration form and a sample follow-up form. All are using dynamic attachements
My sample registration form will generate a unique random numbers, so the numbers generated can be used by data enumerators to continuing the filling of respondents other information.Now, i want my sample registration form to generate a unique random numbers, that doesnt fail, in my sample registration forms attached here in, i want the digit part generated to be atleast 6 numbers and then concatenated with 3 initials of the respondents names. How do i do this, presently, am concatenating the initials with four digits numbers, which to me may fail, if the numbers or character exceed the max allowed limits in the formula provided, which formular could be the most efficient!

aPpQJkhVycLQcqhNS7KNsA.xlsx (8.0 KB)

1 Like

Welcome to the community, Valentine!

Something like this might work (put the part after = in the "calculation" column):

farmer_id_uniq = once(string(int(random() * 1000000)))
gen_farmer_id = concat(
  if(string-length(${farmer_id_uniq}) < 6, "0", ""),
  if(string-length(${farmer_id_uniq}) < 5, "0", ""),
  if(string-length(${farmer_id_uniq}) < 4, "0", ""),
  if(string-length(${farmer_id_uniq}) < 3, "0", ""),
  if(string-length(${farmer_id_uniq}) < 2, "0", ""),
  ${farmer_id_uniq}
)

This ensures that ${gen_farmer_id} is exactly a six-digit number, with leading zeros if necessary.

Edit: after thinking about it more, this is simpler and should also work:

farmer_id_uniq = once(string(int(random() * 1000000) + 1000000))
gen_farmer_id = substr(${farmer_id_uniq}, 1)

This ensures that the random number has exactly seven digits (by adding 1 000 000), then takes the part after the first digit.

Hope this helps!