Article
· Dec 18, 2023 1m read

How to implement sequence without using a ID

If Iris does propose to create keys following a sequence, how can we obtain a sequential number in another context?

In my case, I automatically create care centers, and I want to set them a number like:

APP-DD-999

APP = Name of the application used by the center
DD = center department number
999: sequential number in the department
It is of course possible that centers will be created in competition, so this possible competition must be managed.

My first approach was to use a persistent class and create a new object to retrieve its id, but this involves creating as many classes as departments.

The solution I found involves using a global and the $INCREMENT function. The $INCREMENT function manages concurrency so it's perfect for my use.

So here is my method for obtaining a sequence:

ClassMethod GetSequence(name As %String, item As %String) As %Integer
{
	Return $INCREMENT(^Sequence(name,item))
}

Successive calls to this method provide the expected result:

BNA2024 6d3>w ##class(Ellipse.Utils.PlSandbox).GetSequence("departement",83)
1
BNA2024 6d3>w ##class(Ellipse.Utils.PlSandbox).GetSequence("departement",83)
2
BNA2024 6d3>w ##class(Ellipse.Utils.PlSandbox).GetSequence("departement",83)
3
Discussion (3)2
Log in or sign up to continue