This article covers the details of how autonumbering works on the example of a catalog.
Automatic numbering of catalog items provides unique codes for new items. New items are assigned incrementing numerical codes.
The code generation rules are based on the code series settings specified for the catalog.
- In the entire catalog. All codes in the catalog are unique.
- Within subordination area. Items having the same parent have different codes, while items having different parents can have identical codes.
- Within owner subordination area. Items having the same owner have different codes, while items having different owners can have identical codes.
The autonumbering issues new codes in compliance with catalog code series settings.
Let us tackle an example of a hierarchical catalog with item hierarchy, code series = within subordination area, autonumbering enabled.
Let us add the first item to the catalog:
You can see in the figure that automatic numbering of catalog items starts from 000000001.
Let us add the second item to the catalog:
Let us add one more item that is subordinate to the second item:
You can see in the figure that the series code setting affects the new item code generation: the code for the third item is unique only among the items that are subordinate to the second item.
One of the peculiarities of autonumbering is using the leading zeros. This increases the efficiency of search and sorting by code or by number. To search for items or sort items, the platform uses the database index for the code or number field. In order to use database indexing, 1C:Enterprise adds leading zeroes to any new codes or numbers generated automatically.
Leading zeroes are necessary for proper sorting of documents and other objects that have numbers. For example, without leading zeros a document with number "Doc3" goes after a document with number "Doc11", which is incorrect.
Operations with code prefixes
Adding a prefix to an item code being generated only makes sense if the code is a string.
In order to add a prefix to a new code or number, you can use the On set new code event handler. This event is fired when the code generation begins (for example, when the generation of a catalog item code begins). The handler declaration has the following syntax:
OnSetNewCode (<Standard processing>,<Prefix>),
- <Standard processing> is a flag that shows whether the standard processing is used. If you set this parameter to False in the body of the handler procedure, the standard code generation is canceled.
- <Prefix> is a prefix that will be used for code generation.
Let us tackle an example of a distributed infobase configuration where prefixes ensure the uniqueness of codes in each node. The following script fragment generates unique codes:
// OnSetNewCode event handler
// Changes the code prefix to the unique prefix defined in this infobase
Procedure OnSetNewCode (StandardProcessing, Prefix)
EndProcedure // OnSetNewCode (StandardProcessing, Prefix)
where GetNumberPrefix is an exported common module function, which returns the value of some constant. Each node provides a unique value of this constant:
// Returns a prefix for a new number
// Return value:
// String - prefix for a new number
Function GetNumberPrefix() Export
EndFunction // GetNumberPrefix()
Instead of the On set new code handler, you can use the SetNewCode() method of catalog object. You can pass a prefix as a parameter of this method, in this case the method finds the maximum numeric part among the codes that have this prefix and generates a code with the numeric part incremented by 1. If you omit the prefix, the method finds the maximum numeric part among all codes available in the infobase and generates a code with the same prefix and the numeric part incremented by 1.
Note that for catalogs that have codes of Number type, the prefix returned by the On set new code event handler is ignored.
Autonumbering support by other metadata objects
Apart from catalogs, the following metadata objects support autonumbering:
- Chart of characteristic types
- Business process
To set numbers for documents, business processes, and tasks, use the On set new number event handler.
To set codes for charts of characteristic types, use the On set new code event handler.