A custom number to make a unique ID


(Ahmed Mohammed) #1

Dear All,

I hope you help me for making a unique ID equal 5 numbers with one later to be 6,
For Example 00125A
It is easy in Excel sheet as in the image1
“in excel sheet =TEXT(A2,“00000”)&B2”.


But in CommCare I find more changes, so please how I can make a unique ID when I enter (125) it gives me (00125) with the later(multiple choice) to be (00125A).

Please find the image 2 for more :

Best,
Ahmed


(Ethan Soergel) #2

Hi, Ahmed

I’m not aware of a way to directly pad a number with zeros, but you can make it work with some creativity. This should do what you want for any number between 1 and 99,999:

concat(substr(string(#form/number*0.00001), 2, 7), #form/code)

Here’s how that works:

  • #form/number0.00001 moves the decimal place 5 digits over so you get the correct padding. For example, 1250.00001 is 0.00125
  • string(#form/number*0.00001) converts that to a string so you can use string manipulation functions on it
  • substr(string(#form/number*0.00001), 2, 7) extracts the 5 digits after the decimal place. For example substr(“0.00125”, 2, 7) is 00125
  • And concat is used to attach the code to the end.

Note also that there is a wiki page on generating unique IDs that you may find useful:

https://wiki.commcarehq.org/display/commcarepublic/Generating+a+Unique+ID+for+beneficiaries


(Ethan Soergel) #3

Ahmed,

My apologies - that approach won’t work. Any number below 100 will be converted to scientific notation, so you’d have to make a special case to handle that. Additionally, this approach will strip any trailing zeros. A much simpler approach would be to add 100,000 to your number, then use the substr approach above to drop the leading 1:

concat(substr(string(#form/number+100000), 1, 6), #form/code)

I believe that should work for all numbers from 1 to 99,999.


(Michel Akkaoui - de Sousa) #4

I think an easy way to do so is to register a phone number instead of integer for the digit part of the id, then concat with the letter…
Phone number question type register all numbers, even 0. The concat function automatically convert numbers into strings.

-Michel


(Ahmed Mohammed) #5

Dear Ethan,

Thank you so much … it is very helpful.
it is exactly what I need.

more appreciation
Ahmed


(Ahmed Mohammed) #6

Dear Michel,

Thank you for reply.
Please can you give example, I don’t know how I try it.

Best,
Ahmed


(Michel Akkaoui - de Sousa) #7

When you add a question in a form, you have the choice to select integer, phone number or decimal. just use phone number.

That’s all…

My solution is this :

But Ethan solution works too…

-Michel


(Ahmed Mohammed) #8

Dear Michel,

Thank you …