HOW TO implement an auto increment chrono

Eric BREHAULT edited this page Sep 22, 2016 · 1 revision

How to produce a unique auto-increment chrono for your documents.

If you have a corporate document naming convention, you probably need your document to contain a Reference ID like that: "A0-ACME-001", "A0-ACME-002", "A0-ACME-003", etc.

One part of this reference id being an auto-increment chrono, how to generate it ?

A basic approach could be to count the existing documents and add one to the result, but if some documents are deleted, it migh produce identical ids.

Another basic approach is to get the last numbered document, extract its chrono value, and add one.

But the cleaner approachto implement an auto-increment chrono is to create a specific form that will be use to create a unique document where we will store the current chrono value.

Let's create a form named frmChrono containing a number field named 'chrono'. You create only one doc with this form, and you enter the value 0 in the field.

Then, in your main form (where you need to handle the auto-incremented value), you create a Computed on creation number field (named ReferenceID for instance) with the following formula:

searchchrono=db.getIndex().dbsearch({"Form": "frmChrono"})
# store back the new chrono in the chrono doc
chronodoc.setItem("chrono", newvalue)
# return the reference id properly formatted
return "A0-ACME-%03d" % newvalue

Note: if you use Plomino <1.11, line 3, you need to call getObject() to get the actual document: