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
Solved! Go to Solution.
I approached it a bit differently, but this seem to be working as you expect in both map viewer and Field Maps app.
// 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 from the FeatureSet
var username = GetUser(FeatureSetByName($map, 'מדידת שריפה')).username
var uservals = ''
if (!IsEmpty(fs)){ //make sure not empty dataset
if ($feature.FireEditStatus == 2){
var uservals = Filter(fs, `created_user = '${username}'`) // If Continuing Fire, filter data by username
}else{
var uservals = fs // else grab all data
}
var numarray = []
for (var n in uservals){
Push(numarray, n.fireGisID) // push all matching IDs to array
}
var newID = Max(numarray) // get the max ID number
if ($feature.FireEditStatus == 2){
return newID // If status = 2, return Max existing ID
}else{
return newID + 1 // otherwise, return max ID incremented by 1
}
}
} else {
return $feature.FireGisID; // For other edit types, return the existing FireGisID
}
R_
It appears as if Getuser has some issues with Field maps app and could be your issue.
Take a look here and see if this helps. More here.
R_
Thank you, @RhettZufelt , for the suggestion and the links.
I tried using GetUser as suggested in the post you linked, but the fire incident number still returns 0 in Field Maps instead of the maximum number for that user.
In Map Viewer, the correct number is returned. Do you have any additional ideas for solving this issue?
Here is the updated expression:
// 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 from the portal
var userInfo = GetUser(portal("https://www.arcgis.com"), "");
var username = userInfo.userName;
// Initialize variables
var MaxidStatus1 = 0;
var MaxidForUser = 0;
// 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 created_user matches the current user and update the maximum FireGisID for the user
if (f['created_user'] == username) {
MaxidForUser = Max(MaxidForUser, fireGisID);
}
}
// Determine the new FireGisID based on FireEditStatus
if ($feature.FireEditStatus == 2) {
// If FireEditStatus is 2, return the maximum FireGisID for the current user
return MaxidForUser;
} 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;
}
I approached it a bit differently, but this seem to be working as you expect in both map viewer and Field Maps app.
// 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 from the FeatureSet
var username = GetUser(FeatureSetByName($map, 'מדידת שריפה')).username
var uservals = ''
if (!IsEmpty(fs)){ //make sure not empty dataset
if ($feature.FireEditStatus == 2){
var uservals = Filter(fs, `created_user = '${username}'`) // If Continuing Fire, filter data by username
}else{
var uservals = fs // else grab all data
}
var numarray = []
for (var n in uservals){
Push(numarray, n.fireGisID) // push all matching IDs to array
}
var newID = Max(numarray) // get the max ID number
if ($feature.FireEditStatus == 2){
return newID // If status = 2, return Max existing ID
}else{
return newID + 1 // otherwise, return max ID incremented by 1
}
}
} else {
return $feature.FireGisID; // For other edit types, return the existing FireGisID
}
R_
Thank you so much for your help, @RhettZufelt. The approach you provided indeed resolves the issue, and the calculations are now working correctly in both Map Viewer and the Field Maps app.
I really appreciate your assistance in solving this problem. Your support has been invaluable.
Guy