Select to view content in your preferred language

Mimic Database Sequence with Arcade

334
1
12-27-2024 06:39 AM
Labels (2)
ZachBodenner
MVP Regular Contributor

Hello,

I have a feature class representing right-of-way permits, each assigned a specific permit number. The number format is "YYYY-1" for the first permit of the year, "YYYY-2" for the second, and so on. Right now I have a database sequence enabled for the second portion (the number) and a simple attribute rule that will populate the field PermitNumber with the year and the next sequential permit number. Each year on the first of January I go into the database and restart the sequence so that the first permit of the year will be tagged with a "1". 

What I would like to do is write an attribute rule that mimics this database sequence and the activity of restarting it when the year changes over. Now, I've put more work into this already that the effort it would take to do it manually for the next five to ten years, but I just want to get it to work! Here's my code currently:

// Self-reference the feature class, sort by the most recently created permit
var permits = orderby(featuresetbyname($datastore,'ROW_Utility_Permits'),'CreatedDate DESC')
var latestPermit = First(permits)

// Date of most recent permit
var d = latestPermit.CreatedDate

// Permit number of most recent permit
var p = latestPermit.PermitNumber
console ("Latest Permit Create Date: "+d)
console ("Latest Permit Number: "+p)

// Year of most recent permit
var latestYear = Year(d)
console("Latest Year: "+latestYear)
var currentYear = Year(Now())
console("Current Year: "+currentYear)

// Split the most recent permit number by the hyphen
var sPermitNum = split(p,"-")
var c = Count(sPermitNum)
var num 

// If the split has more than one result (should always), then num equals the value after the hyphen (index 1). In this If/else, the Else should never occur
If (c >1){
num = sPermitNum[1] }
else {
num = 0
}

// Incrementing sequence is the pervious permit number plus 1
var incSeq = ++num
console ("increment: "+incSeq)
var nextPermitNum = ''

// If the year of the most recent permit is the SAME as the permit being created now, then the permit number sequence increases by 1
if (latestYear==currentYear){
nextPermitNum = currentYear+ "-" + incSeq
}

// If the year of the most recent permit is DIFFERENT from the permit being created now, then start the number portion over at 1
else if(latestYear!=currentYear){
nextPermitNum = currentYear+ "-" +"1" 
}
console (nextPermitNum)
return nextPermitNum

 

The console messages imply that the code is working correctly:

Latest Permit Create Date: 2024-12-23T17:58:29
Latest Permit Number: 2024-402
Latest Year: 2024
Current Year: 2024
increment: 403
2024-403

 

However when I go to create a new feature in the feature class the value that gets written to the permit number field is 2024-1. When I create a second feature it's 2024-2. Then every subsequent feature is 2024-2.

Is this a problem with my editing environment or process? I'm working with a copied feature class in a file geodatabase but the real one which I would apply this to is an enterprise geodatabase.

Happy mapping,
- Zach
0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor

The output of the console would certainly indicate it had created the correct permit number. But as you suggest the source of the issue may be down to how you run the code in the attribute rule you had set up. As you do not share a screen shot of this its impossible for people to comment...

0 Kudos