Generate custom IDs for household

Hello everyone,

I’m looking to create a custom unique ID code for new households by concatenating (3 first letters) of the district, site, and a random alphanumeric value.

One approach we’re considering is handling this within the form itself, but I’m concerned about ensuring the uniqueness of the code, as there’s a risk of generating duplicate codes within a given site.

Have any of you encountered similar cases in your projects? If so, how did you address this challenge? I’m particularly interested in learning about your approaches to ensuring code uniqueness within a specific site.

Additionally, I’m curious to know if the CHT currently supports custom transitions for addressing this type of requirement.

I would appreciate any insights or experiences you can share on this topic. Thank you!

Somewhat related to a previous thread: Update existing person documents

The generate_shortcode_on_contacts transition’ does allow you to automatically assign a unique code for each contact. However, at this point there is no way to customize the format of that id code (e.g. if you needed it to be related to the 3 first letters of the place, etc). Outside of this transition, though, it is not going to be possible to guarantee uniqueness of a value generated in a form. The user submitting the form probably does not even have access to check all the relevant docs for uniqueness.

Thinking outside the box a bit, if you were willing to accept a certain probability of uniqueness (proportional to the length of the id code, itself) then it would probably be pretty easy to write a custom extension-lib that could generate id codes of whatever shape you need using something like the uuid package or even just Math.random. This extension could then be used within a form and could even except data from the form as input (e.g. the place names). However, as noted before, there would be no way for offline logic like this to absolutely guarantee that it will avoid collisions.

Also worth noting that you can use the normal ODK form logic to generate random numbers and even uuids. So, an extension-lib is not necessary for basic random generation, but it could be useful if you want a particular format mask.

Thanks @jkuester,
Let me explore these path and give feedback.

1 Like