How to configure a document field to have an unique integer id

Hello,

Is there a way to configure a document field to have unique integer id across all the documents present in the CMS.

We need to set this unique id from backend without user interference.

Regards,
Savan

There are of course several ways of achieving this, but here is one way. You could create a service in the cms to create the ids. Then you create a workflow event listener that listens for a save event, and sets the id if there is none present. If you want this visible then you add that field to document with the document editor, but you set a property mode: view in the field under editor:templates.

@jasper.floor Thanks for your quick reply!!

I am clear on event listener part. Can you please elaborate You could create a service in the cms to create the ids? like the possible ways to generate the unique integer ids.

Regards,
Savan

Depends on the requirements on how you implement a unique integer generator. You could just pick a random number between 1 and 2^63-1 and hope for no collisions. Or you could hand out numbers in sequence. This would require remembering which number was handed out last and using a synchronized block so that only one process can request a number at any time.

Hi @jasper.floor

In our case we are using this unique id as primary key in the database in consumer application(consuming this documents through HALAPI). So we are concerned about collisions.

We thought to create a counter and store current count value as one of the properties on one predefined folder but we are not allowed to create custom property on folder. Do you have any suggestions here or any way to store current count at runtime and upon restart of application also.

Regards,
Savan

You can do this multiple ways. You can create a custom node where you store the information. You can create a mixin if you want to save it on the folder. Adding a mixin will allow you to add extra properties.