Hello everyone,
I'm trying to use an attribute rule to do something that seems relatively simple. I want to take a field with numbers that are incremented by one and have the attribute rule create the next highest number for that field when a new feature is created. I thought that I could just use the Max() function to find the highest number in the list and then simply add a "+1" to calculate the next number.
I'm not interested in the database sequence solution because I want the script to be able to find the maximum value and add one. If I were to create the sequence, it wouldn't necessarily find that value and the numbers could get out of sync.
Has anyone had luck with this? I've looked at several different forums and my script has become more and more complicated for a seemingly simple and probably common task.
I started with:
var lastpolenum = Max($feature.ApexPoleNumber)
return lastpolenum + 1
This was a valid expression but I would get an error that the value was not recognized as a number when trying to create a new feature. ApexPoleNumber is a "Double" numeric field with numbers from 1 to 4779.
After looking at several posts, it seemed like my problem was that I needed to populate array for the Max() function to work and have some work around for a null value occurring. So my script is now looking like this:
var poles = FeatureSetByName($datastore, 'Pole_NumberingTest', ['ApexPoleNumber'], false);
var polenumarray = []
for (var i in poles){
if (poles == null){
return null;}
else{
Push(polenumarray, poles[i]);
}
}
return Max(polenumarray) + 1;
.... kind of seems like overkill but maybe it is the right direction?
This script won't validate because I get an error referring to line 7: "Dictionary type expected".
Any help would be greatly appreciated!
Thanks.
Best,
Ben Paynter