Select to view content in your preferred language

Calculated expression in Field Maps failing in App but works in AGO

1664
3
Jump to solution
10-25-2023 08:03 AM
HaleyBrueckman1
Emerging Contributor

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
}

 

0 Kudos
1 Solution

Accepted Solutions
JustinReynolds
Frequent Contributor

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;
    

 

- Justin Reynolds, PE

View solution in original post

3 Replies
JustinReynolds
Frequent Contributor

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;
    

 

- Justin Reynolds, PE
HaleyBrueckman1
Emerging Contributor

This worked, Thanks!

DFinn22
New Contributor

Would you be able to post the updated script that worked for you.

Thanks,