I have a calculated expression for a field in the forms that I have set up to auto increment an ID number. The arcade I have written sorts the ID field and then takes the highest number and adds one. If there is already a number in this field then it just returns that. This is working when I edit in a web map in AGO or in an experience builder app. But when I tried adding a point in the field maps app on my phone it says that the ID number failed to calculate. I don't understand why it is not working in the app, but is working in AGO.
Here is the Arcade script that is calculating the field:
// Generate FeatureSet item
var fset = FeatureSetByName($map, // Find layer within Web Map
'PointsOfInterest', // Name of layer in map
['ID_Num'], // List of fields to return
false // Do not query geometry
);
// Sort the returned FeatureSet in
// descending order using Asset ID field
var fset_sorted = OrderBy(fset, 'ID_Num DESC');
// Get First Row of sorted FeatureSet
var firstRow = First(fset_sorted);
// Get First Row's Asset ID value
var highestID = firstRow.ID_Num;
// Add 1 to hightest ID for new ID
var newID = highestID + 1;
// If current feature has an Asset_ID
// return the value and don't calculate a new one
// If value is not greater than zero (blank)
// calculate a new Asset_ID and return
if($originalFeature['ID_Num'] > 0){
return $originalFeature['ID_Num']
// return $feature['asset_id'] //THIS WILL CAUSE FAILURE
} else {
return newID
}
Solved! Go to Solution.
Hi Haley,
It is likely failing because $OriginalFeature is a null object when the edit type is "INSERT". $OriginalFeature is something you only want to use when editing an existing feature.
I would wrap the if block where you are using $OriginalFeature in another if where you check the edit context first to see if it is "UPDATE".
Something like the following
...
if ($editcontext.editType == 'UPDATE') {
if (!IsEmpty($originalFeature)) {
if ($originalFeature.id_num > 0) {
return $originalFeature.id_num;
};
};
};
// else return newID
return newID;
Hi Haley,
It is likely failing because $OriginalFeature is a null object when the edit type is "INSERT". $OriginalFeature is something you only want to use when editing an existing feature.
I would wrap the if block where you are using $OriginalFeature in another if where you check the edit context first to see if it is "UPDATE".
Something like the following
...
if ($editcontext.editType == 'UPDATE') {
if (!IsEmpty($originalFeature)) {
if ($originalFeature.id_num > 0) {
return $originalFeature.id_num;
};
};
};
// else return newID
return newID;
This worked, Thanks!
Would you be able to post the updated script that worked for you.
Thanks,