# Creascent invoice number

Hello, im trying to make a creascent invoice number.

I want 7 digits invoice i.e: 0000001

And if: 0000099 when add new one go to: 0000100.

How i can do that? If i try save number: 00000001, it only saves 1.

Thanks.

Invoice number = this invoice number +1

If i try save number: 00000001, it only saves 1.

Im not asking how add +1 to last value, im asking how i can save 7 digits value using 0.

1 Like

Just keep in mind if you do incremental counts that if you permanently delete a record, you may inadvertently create duplicate invoice records.

For this purpose, itâ€™s good to have some ongoing ledger. Or to have a mechanism to mark invoices as user deleted, but not actually deleted from the database.

Also, you want to format it as a text (numbers only). If you save it as a number, leading zeros will be omitted.

1 Like

Hi therealone,

I do these kinds of problems as follows:

1. You need not one, but two fields, the first of type number, the second of type text.

2. You do the calculations and incrementations on the number field only.

3. Once you have the right n-digit number for your invoice, you transform it into a n-letter text element.

4. You take the n-letter text element and superpose it on the last n zeros of the generic text element â€ś0000000â€ť, for instance â€śxyzâ€ť and â€ś0000000â€ť gives â€ś0000xyzâ€ť

5. You store the text â€ś0000xyzâ€ť in the text field.

Example: extract the invoice number 67 from the number field => transform into text element â€ś67â€ť => superpose the two texts â€ś0000000â€ť and â€ś67â€ť right-aligned to give â€ś0000067â€ť => store the latter in the text field.

Note that numbers and texts are different formats of variables, you cannot mix them, but have to transform one into the other before doing operations on the pair. The text â€ś0000000â€ť transformed into a number gives 0, but the number 0 transformed back into a text gives â€ś0â€ť.

Hope this helps, good luck,

O.

1 Like

Oh sorry I quick reply, didnâ€™t really pay a lot of attention.

Very good added points. Second to what you said

But if you add a value to last record, doesnâ€™t matters if he deletes any record.
If he deletes the last one, we will replace it with new one.

And if he deletes an old one, he will keep adding new records using last so that shouldnt be a problem.

Yes, what youâ€™re saying is correct. So long as you are referencing a discrete field on the last invoice, you shouldnâ€™t encounter unintended duplicates. @ones comment clarified the point.

My initial comment was based on the logic of a :count, function.

Let me jump in here: how do you superpose? I have never noitced a â€śsuperposeâ€ť option anywhere.

I had the same problem. I just store the invoice number in the database as a number which makes it easy to increment and use in searches. I always keep the last used invoice number in a database as well. Then when I need to display the number, like in a text field, I do zero padding as follows:

First, take the invoice number and add a number thatâ€™s one larger than the maximum possible invoice number. Example: Add 10000000 to invoice number to represent an invoice number from 1 to 9999999.

Then format the invoice number as a number with 0 decimal places. This turns the number into a string.

Finally, extract the number with the leading zeros using Regex.

Hope this helps.

BTW: The Regex pattern .{7}\$ will extract the 7 least significate digits. Just change the number to match the number of digits to extract from the number string.

8 Likes

This topic was automatically closed after 70 days. New replies are no longer allowed.