Skip to content
Firmspace
Back to blog

The Number the AI Is Not Allowed to Invent: How Firmspace Numbers Documents

Ask a small firm what its invoice number was last month and most people will remember the format, not the digits. Ask an accountant the same question and the answer is a sequence of documents that must line up. A number is the smallest field on a document and the one that is hardest to repair once it is wrong.

In Firmspace CRM a document number is built from three parts: a prefix, the year and a counter that starts again every January. An invoice created today looks like RE-2026-1, the next one RE-2026-2, and a credit note from the same firm runs in its own series, for example GS-2026-1. The prefix is not decoration; the counter is kept per prefix and per year.

The counter itself lives in the firm's finance settings and is advanced by a single database statement that both increments the value and returns it. That detail is the difference between a quiet system and a support ticket. Reading the counter, adding one and writing it back gives two simultaneous requests the same number; incrementing and returning in one locked statement gives each request its own.

The year is part of the key, so on the first working day of January the sequence begins at one again while every number issued in December keeps its meaning. Old documents are not renumbered and are not migrated to a new scheme. Their numbers are historical facts.

Prefixes come from two places, and the order matters. An active document template can define the prefix for invoices, quotes, credit notes, delivery notes and acts; if the template does not define one, the firm's finance settings are used. Some documents do not consult either: sales orders use SO, contracts use CT, and purchase orders and production orders carry their own fixed prefixes. Changing a prefix therefore starts a new series for that prefix in the current year rather than continuing the old one.

A number is assigned when the document is created, not when it is sent. A draft invoice already holds its number, which surprises people who expect the number at the moment of sending. It also means that deleting a draft does not return the number to the pool: the counter never moves backwards, so the gap stays and the number is never handed to another customer.

Before a number is accepted, the system checks that it is not already used by an invoice, a quote, an order or a contract. This matters after an import, a manual correction or a migration from another system. If the candidate is taken, it is skipped and the counter moves on; the loop allows up to two hundred attempts before it gives up with an error instead of guessing.

For the AI assistant, this is the whole point. When someone asks it to create an invoice, a quote, an order or a contract, the model supplies the customer, the line items, the dates and the currency — never the number. The number is requested from the same server function that every button uses. A language model is good at language and unreliable at uniqueness; the design keeps it away from the second job.

The assistant is also how many people meet a document number without ever looking at a PDF. It can find an invoice by the number a person says out loud, including a number typed in Cyrillic letters that look like Latin ones: РЕ-2026-5 and RE-2026-5 are treated as the same reference. A short tail such as 2026-5 is accepted too. If several documents match, it does not choose silently; it lists the candidates and asks.

Numbers are used for finding, not for editing. The assistant can change an invoice's dates, customer name or notes, and it can delete an invoice, but it cannot renumber one. The audit log keeps the previous values of an edit and a full snapshot of a deleted invoice. Identity is the one thing the assistant is not allowed to touch.

The same discipline applies to copies. Duplicating an invoice creates a new draft with the same customer and lines and a fresh number from the invoice series. A recurring invoice gets a new number on every run, because each run is a new document, not a new version of the old one. A credit note is never duplicated: the product refuses it, because a correction is tied to exactly one original.

Gaps are the honest part of the story. A skipped candidate, a deleted draft or a document imported from another system can leave a hole in the series. A gap is visible, but it is not a duplicate, and the alternative — reusing a number — is far worse. The system prefers a hole to a collision.

What a firm should decide once, at the beginning: the invoice prefix, the quote prefix, the credit note prefix and whether a template should override them. Then leave the sequence alone. A number is a promise that two different documents will never be confused, and the promise is easiest to keep when nobody edits the settings between two invoices.

None of this is glamorous, and that is the point. The AI can read a request, draft a document and find it again by a half-remembered number. The number itself comes from a counter in a database, because that is the only place where uniqueness can be guaranteed. Good infrastructure is boring exactly where the business needs it to be.

Let us automate your next process together.

Describe a recurring workflow to us. Together we will check whether and how it can be automated sensibly.