Implement an ID generator?
In a few use cases, internal stakeholders have requested to create a human-friendly identifier with an incrementing sequence. For example, if creating an inventory of use cases, they would like to see “UC-00157” rather than “45fac3af-1bf3-4805-bf5a-9458f9a8a73e”.
This seems relatively straightforward to implement in a workflow, but where to store the stateful sequence? At the moment, I feel my best bet is to store this information as part of a workflow configuration. So one workflow would be called “Stateful configuration” and would store all the info needed for all the other workflows.
The best part is I can redeploy it without overwriting the existing configurations.
Any thoughts or ideas?
def variable = 'useCaseSeq'
def cfg = workflowDefinitionApi.getWorkflowDefinitionByProcessId('ucbConfiguration')
def seq = cfg.configurationVariables.get(variable).toInteger() + 1
def code = "UC-" + seq.toString().padLeft(5, '0')
workflowDefinitionApi.changeWorkflowDefinition(
ChangeWorkflowDefinitionRequest.builder()
.id(cfg.id)
.configurationVariables([variable: seq.toString()]).build()
)
arthurburkhardt
Posted 5 years ago · Edited 1 year ago·Last reply 3 years ago
17 comments
arthurburkhardt
OP3 years ago · Editedhum… have you checked if the script task are correctly configured as groovy and not javascript?

Tyler Bass
·3 years ago · EditedYes, the script task is is correctly configured as groovy.
Given your use of the word “are,” you are making me feel inclined to try to split my script task into two: One to define the assetName and another to add the asset and the attributes.
arthurburkhardt
OP3 years ago · EditedYou’re supposed to use the issueId as the name of your issue, otherwise it just doesn’t work. => You can adapt the logic to whatever else you want.
Also, I recommend against using the issueApi => There’s a lot of hidden hardcoded logic. For example, you can’t report on issues (in hidden issue domains) in the usage analytics dashboards.
shubhangishekhar
·3 years ago · EditedHi @arthur.burkhardt
I have utilized the same code you shared for generating id whenever a new issue gets created . I am seeing same Issue ID generated with each time a new issue gets created. So seek your help to avoid duplicate IssueID creation. For reference attaching the code here:
def requesterId = userApi.getUserByUsername(requester).getId()
def descriptionString = execution.getVariable(“description”)?.toString() ?: “”
def communityId = responsibleCommunity
def relatedAssets = execution.getVariable(“relatedAssets”) ?: []
def addAttributeToAsset(assetUuid,attributeValue,attributeTypeUuid) {
if (attributeValue == null){
return;
}
attributeApi.addAttribute(AddAttributeRequest.builder()
.assetId(assetUuid)
.typeId(string2Uuid(attributeTypeUuid))
.value(attributeValue.toString())
.build())
}
def relatedAssetsList = []
relatedAssets.each{ relatedAssetId ->
def relatedAssetRef = RelatedAssetReference.builder()
.assetId(relatedAssetId)
.direction(true)
.relationTypeId(string2Uuid(impactsRelationId))
.build()
}
def namePrefix = “ISSUE-”
def seq = 1 + (
assetApi.findAssets(FindAssetsRequest.builder()
.name(namePrefix).nameMatchMode(MatchMode.START)
.sortField(SortField.NAME).sortOrder(SortOrder.DESC).limit(10).build()
).results
.collect{(it.name =~ /${namePrefix}(\d{5})/).collect{it}.flatten()}
.collect{whole, seq -> seq.toInteger()}.find()?:0
)
loggerApi.info(“THE SEQUENCE NUMBER IS: ${seq}”)
def issueId = namePrefix + seq.toString().padLeft(5, ‘0’)
loggerApi.info(“THE ISSUE GENERATED IS: ${issueId}”)
def newIssueUuid = issueApi.addIssue(AddIssueRequest.builder()
.name(subject)
.description(descriptionString)
.priority(priority)
.responsibleCommunityId(communityId)
.relatedAssets(relatedAssetsList)
.categoryIds(classifications)
.typeId(string2Uuid(dataIssueId))
.requesterId(requesterId)
.build()
).getId()
addAttributeToAsset(newIssueUuid,issueId,issueIdTypeId)
addAttributeToAsset(newIssueUuid,issueClassificationCategory,issueClassificationCategoryAttributeTypeId)
execution.setVariable(“outputCreatedTermId”,uuid2String(newIssueUuid))
shubhangishekhar
·3 years ago · EditedHi Team
In my scenario I have to generate data issue asset in sequence. I am able to generate the sequence number but the same Id gets created again (e.g: All issues have issue id as ISSUE-00001). How to resolve it and make each sequence unique.
arthurburkhardt
OP3 years ago · EditedHey @shubhangi.shekhar.tietoevry.com,
You should try to pinpoint your issue and share the specific code if you hope anyone can help.
arthurburkhardt
OP4 years ago · EditedFor reference, I gave up the initial ID generator implementation because changing configuration variables was updating workflows every time.
Instead, I ended up just querying last results and incrementing the highest asset number.
pros: Performance, easy to code, easy to maintain, low risk of errors
cons: risk of reusing ID (in case asset is deleted)
The following code does:
The reason why 10 assets are looked up is to avoid the risk that incorrectly named assets could overshadow the last element of the sequence. (e.g. ABC-Z0001 would cause errors.)
If there is no risk for incorrectly named assets, you could adjust the
limit(10)tolimit(1)or whatever you like.arthurburkhardt
OP4 years ago · EditedFor reference, I gave up the initial ID generator implementation because changing configuration variables was updating workflows every time.
Instead, I ended up just querying last results and incrementing the highest asset number.
pros: Performance, easy to code, easy to maintain, low risk of errors
cons: risk of reusing ID (in case asset is deleted)
The following code does:
The reason why 10 assets are looked up is to avoid the risk that incorrectly named assets could overshadow the last element of the sequence. (e.g. ABC-Z0001 would cause errors.)
If there is no risk for incorrectly named assets, you could adjust the
limit(10)tolimit(1)or whatever you like.Tyler Bass
·3 years ago · EditedI am trying to implement an ID generator that only takes the highest primary-key value (just a number like “328”) in a domain and adds one to it. The console is providing somevague, negative feedback when trying to upload a workflow with the code following this paragraph. If you would have any constructive critiques, please, I would appreciate that. Thanks.
arthurburkhardt
OP3 years ago · EditedDid you import all the required classes (FindAssetsRequest, SortOrder, etc.)?
If you can paste the logs, it would be useful to troubleshoot.
stefan_busch
·3 years ago · Editedwouldn’t you need to call assets.getResults() to get the results (instead of assets.result)
arthurburkhardt
OP3 years ago · EditedNo, what @tyler.bass wrote is perfectly valid, this is the groovy shorthand and I prefer that syntax as well, that’s what I always use.
This is applicable to all the getter methods
stefan_busch
·3 years ago · Editedthanks for the info Arthur!
Tyler Bass
·3 years ago · EditedThis is what has been coming out of the console:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.collibra.dgc.core.model.user.impl.RoleImpl.rightsSet, could not initialize proxy - no SessionAhead of my script attempting to add assets and attributes, along with the ID-generation functionality, I’ve been importing like so:
Again, thanks for your help.
Ronald Schaareman
·5 years ago · EditedHi @arthur.burkhardt,
We had the same kind of request actually and Collibra built a custom workflow for us. In our case it was about change request (CR) numbering and the CR number (#) would be assigned as the Asset Name (format CR#) .
The workflow would substring all the CR numbers in the assigned domain, put it in a list, find the max number and create the next number (# + 1).
Maybe this gives you an idea.
Kind regards, Ronald
bartvanderlocht
·5 years ago · EditedIn the past, we implemented something similar with a ‘hidden domain’:
-create hidden domain (i.e. a domain only visible for sysadmins)
-create one asset per ‘counter’ you need to have
-modify (increment) the name of the counter-assets
Additionally, we had one ‘batch-workflow’ which could generate multiple keys at once. This is helpful when assets are also created outside of workflows (e.g. by excel-imports)
adityapawar
·5 years ago · EditedHi Arthur,
Previously when I have asked to build such requirement at that time I have implemented it by incrementing count of assets as mostly assets of that type are represented by different unique asset type and we never give option to delete assets to any user so you are just adding new assets that used to work for me.