Generating a Unique ID for beneficiaries

Generating a Unique ID for beneficiaries

Many programs assign their beneficiaries externally-facing IDs. Using a combination of custom user data and a counter you can assign unique IDs to your beneficiaries. Since CommCare works offline you must create a portion of the ID that is based on the user case, since each user can be working offline simultaneously. 

Note on Case IDs versus Unique IDs

CommCare assigns every case a globally unique case ID that is a 36-character long alphanumeric string. You should always use the case ID to link your data on the backend. These instructions detail how to create an easily readable user-facing ID for beneficiaries. This should not be used to link data on the backend or for any data analysis.

 

Generating a Unique ID

Step 1: Create a property in custom user data

  • Create https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143958236 property for each mobile user called "prefix"

  • Assign each user a prefix value, for example, 10, 11, 12, 13, 14 and so on. In order to determine your prefixes, you need to think about how many beneficiaries you expect each mobile user to register. In this example, we do not expect the user to register more than 10,000 beneficiaries, ever, and our prefixes and suffixes match that. 

    • You can assign this prefix when you add the mobile users individually, or in bulk. 

    • Notably, this prefix can also be a letter or a code, it does not have to be numeric.

Step 2: Create a counter in your registration form

Step 3: Create a hidden value to generate the unique ID

Example

Let's look at an example of how this would work in an app.

  • In step 1, my user case property is called prefix.

  • In step 2, the counter that I made is saved as the property child_count.

  • So finally, in step 3, the calculation that I would put into my beneficiary_id calculate condition box would be:

if(#form/child_count < 10, concat(#user/prefix, '000', #form/child_count), if(#form/child_count < 100, concat(#user/prefix, '00', #form/child_count), if(#form/child_count < 1000, concat(#user/prefix, '0', #form/child_count), "")))

If my user prefix was 12, and I registered my 5th child of the day, the ID that CommCare would generate for this child ID would be: 120005.