Existing attribute gets overwritten with calculated field

210
2
02-27-2024 01:05 PM
AdamAull
New Contributor II

Using the map viewer in Portal 11, I have added an editable feature service and created a edit form. The form fields use Arcade expressions to calculate field values. The expression works great, adding a new, incremented ID to each additional feature created. But when existing features are edited, the expression increases the current ID to the next available ID.

I've looked into using $originalFeature.ViolationID to compare with $feature.ViolationID of the selected feature. It works in the Test mode but fails to return anything at all in production. I've even removed all the code and just put in the code here. Instead of returning 0000001, the field ends up blank.

 

 

var org_id = $originalFeature.ViolationID
var new_id = $feature.ViolationID

return "00000001"

 

 

 

Is there any way to get a calculated field in a Portal map viewer editing form to not overwrite existing values when updating selected features?

 

Working Expression that overwrites existing ViolationID

 

 

var current_year = Number(Right(Text(Year(Today())), 2))
var ids = FeatureSetByName($map, "Arcade_Test", ["ViolationID"], false)
var id_list = []

for(var id in ids){
    Push(id_list, id["ViolationID"])
}

Sort(id_list)
var list_length = Count(id_list)
var highest_id = id_list[list_length - 1]
var year_check = (Left(id_list[list_length - 1], 2))
year_check = Number(year_check)
var next_id = 1

if(Number(current_year) > year_check){
    return current_year + Text(next_id, '00000')
}

next_id = Number(highest_id) + 1
return next_id

 

 

 

0 Kudos
2 Replies
ChristopherCounsell
MVP Regular Contributor

You need to put a 'null' handler in there or the console will throw an error.

https://community.esri.com/t5/arcgis-field-maps-questions/form-calculation-with-originalfeature-retu...

https://community.esri.com/t5/arcgis-field-maps-questions/calculated-expression-in-field-maps-failin...

You can also consider using the $editcontext.editType in an if statement. The above posts have a working example.

0 Kudos
AdamAull
New Contributor II

Thank you Christopher. That really helps explain some things. I've implemented the $originalFeature solution and run some testing. The expression appears to work correctly when the first feature selection and update is made or when the first new feature is created. Then it gets stuck and each additional feature update or creation gets overwritten. This occurs until a refresh of the map resets the $editcontext.editType. It's as if the editType keeps the UPDATE or INSERT in memory during the length of the edit session.

I've cleaned up the expression and included it below. Line 20 was my attempt to solve the editType issue not being checked. But that doesn't appear to be the problem. Is there a way to force the editType to refresh  during a single editing session without refreshing the browser and beginning another session?

 

var current_year = Number(Right(Text(Year(Today())), 2))
var viols = FeatureSetByName($map, "Arcade_Test", ["ViolationID"], false)
var viols_ordered = OrderBy(viols, 'ViolationID')
var id_list = []

for(var viol in viols_ordered){
    Push(id_list, viol.ViolationID)
}

var highest_id = Number(Back(id_list))
var year_check = Number((Left(highest_id, 2)))

if($editcontext.editType == 'UPDATE'){
    if(!IsEmpty($originalFeature)){
        if(Number($originalFeature.ViolationID) > 0){
            return $originalFeature.ViolationID
        }
    }
}
else if($editcontext.editType == 'INSERT'){
    if(year_check > current_year){
        return Text(current_year) + Text(1, "00000")
    }
}
return highest_id + 1

 

0 Kudos