Hi,
I've got a smart form that autopopulates tree numbers for points. It queries the featureset for other tree points with the same project number (or no project number), sorts the values, and then adds 1 to the largest one.
In the map viewer, when I add a new point, it correctly calculates the tree number based on the project number field, and if I update the project number, it recalculates the tree number as expected.
In the Field Maps mobile app, on the other hand, the form does not recalculate the Tree Number field when I update the Project Number field. In fact, it calculates the Tree Number field based on a null Project Number value since that's what was present when the form was opened. Entering or changing the Project Number value has no effect on the Tree Number field.
Our script works fine when we calculate the project number field (we often set up separate maps with the project number automatically calculated for individual projects to save time in the field), but we also want to have a more flexible map & form that can be used to collect data across multiple projects, so field staff will enter the project number manually (side note: I really wish I could opt to cache the value they enter in that field for them like I can in the Survey123 web designer so they only have to enter it once and don't enter the project number inconsistently!).
Does anyone know why it's behaving differently in the mobile app than it is in the map viewer?
Here's my code for reference (I have likely overcomplicated it trying to resolve this issue):
var nextTreeNumber = 1
// If this feature already has a Tree Number, return that
if(!IsEmpty($feature.TreeNumber)) {
return $feature.TreeNumber
}
// Get the project number
var projectNumber = trim(upper($feature.ProjectNum))
// Otherwise, get tree number values for the whole Arborist Template featureset
// Check that the featureset isn't empty
if (!IsEmpty(first($featureset))) {
// Query the featureset for trees with the same project number (even if it's null)
if (isEmpty(projectNumber)) {
var projectTrees = filter($featureset, "ProjectNum IS NULL")
} else {
var projectTrees = filter($featureset, "ProjectNum = @projectNumber")
}
if (!IsEmpty(first(projectTrees))) {
var maxTreeFeature = First(OrderBy(projectTrees, "TreeNumber DESC"))
// if there are no trees with the same project number, return 1
if (isEmpty(maxTreeFeature.TreeNumber)) { return nextTreeNumber }
// if there are trees with the same project number, add 1 to the highest one
var maxTreeNumber = maxTreeFeature.TreeNumber
nextTreeNumber = maxTreeNumber + 1
return nextTreeNumber
} else { return nextTreeNumber }
} else { return nextTreeNumber }
Thanks!
Holly
Solved! Go to Solution.
Okay, yep, that worked. Here's the replacement code for those following along at home (or more likely for myself in six months when I have the same problem and forget that I already figured it out once before). This change makes Field Maps leave the tree number alone for existing points that are being updated, but allows it to update the tree number when the project number changes in the form for new points.
// If this is an existing feature with a Tree Number, return that
if ($editcontext.editType == 'UPDATE') {
if(!IsEmpty($feature.TreeNumber)) {
return $feature.TreeNumber
}
}
Update:
I figured out that the issue is the check at the beginning to return the existing tree number if there already is one.
// If this feature already has a Tree Number, return that if(!IsEmpty($feature.TreeNumber)) { return $feature.TreeNumber }
For some reason, in the Map Viewer, this doesn't prevent the tree number from updating when I create a new point and then enter a value in the Project Number field, but in the mobile app it does. If I comment this part out, the tree number updates as expected when I enter the project number. But I need this part so it doesn't change existing tree numbers when existing points are edited. So I'm still stuck and suggestions are welcome! I'm probably going to start by trying to use one of the $editcontext.editType parameters...
Okay, yep, that worked. Here's the replacement code for those following along at home (or more likely for myself in six months when I have the same problem and forget that I already figured it out once before). This change makes Field Maps leave the tree number alone for existing points that are being updated, but allows it to update the tree number when the project number changes in the form for new points.
// If this is an existing feature with a Tree Number, return that
if ($editcontext.editType == 'UPDATE') {
if(!IsEmpty($feature.TreeNumber)) {
return $feature.TreeNumber
}
}