Howto create an auto-increment field on an object in Alfresco

Background: Let’s say you have a datalist that you use in Alfresco Share. Sometimes you want to have an auto-increment column in the datalist table, that automatically increases by one in order to create a unique id per row in the table, much like an auto-increment primary key column in a database.

For this howto we’ll use the Alfresco datalists as the practical example, but the approach is general and can be used in other application areas too.

  • Identify the parent node of the collection of objects that you want the auto-increment to apply to. In this case it would be the datalist node.
  • In your data model definition, add a d:int column to hold the increment value and set the ”countable” aspect as mandatory. E.g:

 

  • In your Share form config (in share-config-custom.xml), set the auto-increment ID field to read only for the edit form config. (Don’t include the ID field at all in the create item form.) So for edit form we have e.g:

 

  • Next, we need to run Java code when a new item is created. This can be done by using policies and behaviour, but for this example we’ll instead intercept the Share form post on the Alfresco repo side by defining our own version of TypeFormProcessor.java. Let’s say our form processor is called AvantecFormProcessor.java, and we register it like this (in form-services-custom-context.xml):

 

  • In Share client side JS, you then make sure the form templateUrl has the right itemType set for the form post:

 

  • Finally, at the end of the internalPersist(…) method of AvantecFormProcessor.java, we set and increment the counter on the posted datalist item:

 

 

En reaktion på ”Howto create an auto-increment field on an object in Alfresco

  • I have need of auto increment in data lists in Alfresco and stumbled upon your article. But I am a beginner in Alfresco and do not know all the things needed in order for it to work.If you could give me a little more guidance I would be really grateful

    Which file is this mentioned in the class property
    se.avantec.module.example.forms.processor.AvantecTypeFormProcessor ?
    Where should the *.js be inserted in Share ?
    What is supposed to be written in the *.js file – things like – templateUrl and the … ? Where exactly templateUrl is pointing to?
    Is myType something like integer or something else?
    And what should we write in the AvantecFormProcessor.java so it will work? You just say that we need to insert the code in the class in a metod called internalPersist, but at the end where?
    The class is empty at the beginning, so what should we write there?

    Thanks in advance

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *