CMDBuild Forum

Autogenerated key

Hi,
in the documentation I am looking for a possibility to fill a field automatically with a value.

I found in Manual the “Auto value” but this can only genereate a value from other attributes of the form.

Any idea how to generate a unique key?

Regards Philipp

Hi, I faced the same issue.
With my knowledge you cannot do this from UI, but I used a workaround using Postgresql capabilities. This is what I did since CMDB 2.5:
a. define a Postgres sequence i.e. my_seq
b. define a trigger function on the class you need that handle the myseq to build the value you want. i.e. if you want a unique Code like PR-YYMM9999

vCodice = RIGHT(to_char(EXTRACT(YEAR FROM now()),‘0000’),2) || RIGHT(to_char(EXTRACT(MONTH FROM now()),‘0000’),2) || (select lpad(nextval(‘my_seq’)::varchar, 4, ‘0’));
NEW.“Code” = ‘PR-’ || vCodice;

1 Like

Thanks for the tip that should work.
Regards Philipp