Hi,
I'm encountering an issue with a calculation expression that works correctly in Map Viewer but not in Field Maps. I have an expression that computes a fire incident number automatically based on the fire's status. When the fire status is 1 (new fire), the calculation finds the maximum number in the layer and adds one. When the fire status is 2 (continuing fire), the calculation finds the last fire number for the same user.
In Map Viewer, the calculation works perfectly. However, in Field Maps, I'm experiencing two issues:
For status 1, I get a number smaller than the maximum number.
For status 2, I get 0 instead of the expected number.
Here's the expression I'm using:
// Check if the edit type is 'INSERT'
if ($editcontext.editType == 'INSERT') {
// Access the FeatureSet of the relevant layer with required fields
var fs = FeatureSetByName($map, 'מדידת שריפה', ['FireEditStatus', 'FireGisID', 'created_user'], false);
// Get the current user's username
var username = GetUser($layer).username;
// Initialize variables
var MaxidStatus1 = 0;
var MaxidStatus2 = 0;
var hasStatus2 = false;
// Iterate through the features in the FeatureSet
for (var f in fs) {
var fireEditStatus = f['FireEditStatus'];
var fireGisID = Number(f['FireGisID']);
// Check if the FireEditStatus is 1 and update the maximum FireGisID
if (fireEditStatus == 1) {
MaxidStatus1 = Max(MaxidStatus1, fireGisID);
}
// Check if the FireEditStatus is 2 and the created_user matches the current user
if (fireEditStatus == 2 && f['created_user'] == username) {
hasStatus2 = true;
MaxidStatus2 = Max(MaxidStatus2, fireGisID);
}
}
// Determine the new FireGisID based on FireEditStatus
if ($feature.FireEditStatus == 2) {
// If FireEditStatus is 2, return the maximum FireGisID for the current user
if (hasStatus2) {
return MaxidStatus2;
} else {
return null;
}
} else if ($feature.FireEditStatus == 1) {
// Increment the maximum FireGisID by 1 for status 1
return MaxidStatus1 + 1;
} else {
return null;
}
} else {
// For other edit types, return the existing FireGisID
return $feature.FireGisID;
}
Has anyone experienced similar issues with expressions in Field Maps, and what solutions might you suggest?
Thank you for your assistance!
Guy