I am attempting to calculate a unique ID in a form for newly-added features. The unique ID should conform to the pattern YYYYddddd where YYYY is the current year and ddddd is a five-digit integer sequence denoting the order in which each feature was added (e.g., 202400001). When editing an existing feature, I'd simply like the calculation to return the existing value.
My calculated expression currently looks like this:
if ($editcontext.editType == 'UPDATE') {
if (!IsEmpty($feature["treeID"])) {
return $feature["treeID"]
}
else if ($editcontext.editType == "INSERT") {
return Number(Concatenate(Year(Today()),Text(Count(Filter($featureset,left($feature["treeID"],4)+'='+Year(Today()))) + 1,'00000')))
}
}
Solved! Go to Solution.
@DanielBurcham
Could you try this?
if($editcontext.editType=="INSERT" || ($editcontext.editType == "UPDATE" && IsEmpty($feature.treeID) ))
{
#code to calculate the unique ID
}
else {$feature.tree.ID}
Refer to existing values as $originalFeature otherwise it's an self intersecting calculation loop
if ($editcontext.editType == 'UPDATE') {
if (!IsEmpty($feature["treeID"])) {
return $originalFeature["treeID"]
Not sure if you also need to refer to the original feature in !isempty, test that if it doesn't work
Thank you for the suggestion! The change does not seem to work. I tried using 'originalFeature' in both ways, but I still receive an error when creating a new feature. Though, it seems to work fine when editing existing features.
@DanielBurcham
Could you try this?
if($editcontext.editType=="INSERT" || ($editcontext.editType == "UPDATE" && IsEmpty($feature.treeID) ))
{
#code to calculate the unique ID
}
else {$feature.tree.ID}
It worked! Thank you for the solution. For anyone interested, here's the final version of my working code:
if ($editcontext.editType == 'INSERT' || ($editcontext.editType == 'UPDATE' && IsEmpty($feature["treeID"]))) {
return Number(Concatenate(Year(Today()),Text(Count(Filter($layer,"CREATIONDATE > DATE '2024-01-01'")) + 1,'00000')))
}
else {
return $originalFeature["treeID"]
}
Can someone help me please? The above example works 100% but I want to add a text prefix to the output.
Example: ABC2020400001
Appreciate your help.
Thanks