Select to view content in your preferred language

Calculating unique ID field using Arcade

435
6
Jump to solution
07-17-2024 12:44 PM
Labels (1)
DanielBurcham
Emerging Contributor

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')))
}
}
 
The Arcade code works during testing in the editor, but I receive an error (attribute failed) when attempting to add a new feature and automatically calculate the field using the form. 
 
I would appreciate any help or advice to solve the issue! 
0 Kudos
1 Solution

Accepted Solutions
gis_KIWI4
Frequent Contributor

@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}

 

 

View solution in original post

0 Kudos
6 Replies
ChristopherCounsell
MVP Regular Contributor

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"]

0 Kudos
ChristopherCounsell
MVP Regular Contributor

Not sure if you also need to refer to the original feature in !isempty, test that if it doesn't work

0 Kudos
DanielBurcham
Emerging Contributor

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.

0 Kudos
gis_KIWI4
Frequent Contributor

@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}

 

 

0 Kudos
DanielBurcham
Emerging Contributor

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"]
}
0 Kudos
StephanLe_Roux
Emerging Contributor

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

0 Kudos