I am trying to use the below Arcade code to generate a new incremental ID value for features in a layer when edited in Experience Builder. Features are typically submitted via Survey123 with a default value of "NEW". Once submitted, our OHS people review them in Experience Builder and edit the feature to show they have done so. The below is a calculation applied in Field Maps Designer that "should" check all features in the layer, find the highest existing ID value (e.g. HAZ - 095) and calculate a new ID value (e.g. HAZ - 096). This also needs to happen if a new feature is created in Experience Builder (which would have an empty value instead of "NEW"). If a feature has been previously edited and has an "HAZ - ###" value, then it should be retauned.
Can anyone show me whats going wrong? Currently all NEW or blank values are being calculated as "HAZ - 001" and exisiting values are being returned correctly.
// Fetch the current value of the field
var currentValue = $feature["Hazard_ID"];
// Check if the current value is "NEW" or blank
if (currentValue == "NEW" || IsEmpty(currentValue)) {
// Fetch all features to determine the latest number
var features = FeatureSetById($map, 'Form FPC94 Hazard Report - OSHE Review', ['Hazard_ID'], false);
var maxNumber = 0;
// Iterate through features to find the highest number
for (var f in features) {
var value = f["Hazard_ID"];
if (Find("HAZ - ", value) == 0) {
var number = Number(Mid(value, 6));
if (number > maxNumber) {
maxNumber = number;
}
}
}
// Increment the highest number by 1
var newNumber = maxNumber + 1;
// Format the new number with leading zeros
var formattedNumber = Text(newNumber, "000");
// Return the new value
return "HAZ - " + formattedNumber;
} else {
// Return the current value if it's not "NEW" or blank
return currentValue;
}