Select to view content in your preferred language

Arcade Auto Calculate AssetID for New Feature

164
1
Jump to solution
4 weeks ago
Labels (3)
HHight
by
Occasional Contributor

I am working on an arcade script to auto calculate the next sequential AssetID for any new features. The existing features all have a unique AssetID in the form: MH.12345

The script I have so far is being plugged into the smart form for the feature as a calculation for the field AssetID. The idea is to ONLY have an assetid generated when a new feature is created. The problem I  have is when I go to edit a feature with an existing assetid, the assetid is wiped and set to a null.

My code is below:

 

 

// If this feature already has an assetid, return the existing assetid
if (!IsEmpty($feature.assetid)) { 
    return $feature.assetid 
}

// Verify if this is a new feature
if ($editcontext.editType == "INSERT") {
    // get the feature with the greatest assetid
    var id_features = Filter($layer, "assetid IS NOT NULL")
    var max_id_feature = First(OrderBy(id_features, "assetid DESC"))

    // For an empty featureset, return the first assetid in the dataset
    if (max_id_feature == null) { return "MH.00001" }

    // Calculate and return the next assetid
    var max_id = max_id_feature.assetid
    var next_id_number = Number(Replace(max_id, "MH.", "")) + 1
    return "MH." + Text(next_id_number, "00000")
}

// If none of the above conditions are met, return null to do nothing
return null

 

 

 

What am I missing that would allow me to insert the next assetid but not touch or modify existing assetid values?

Thanks in advance.

 

UPDATE:

I also just updated the code to include an if statement for editType == "UDATE" and the script is still behaving the same. Code is below:

 

// If this feature already has an assetid, return the existing assetid
//if (!IsEmpty($feature.assetid)) { 
//    return $feature.assetid 
//}
// If the feature is being modified, return the existing assetid
if ($editcontext.editType == "UPDATE") {
    return $feature.assetid
}
// Verify if this is a new feature
if ($editcontext.editType == "INSERT") {
    // get the feature with the greatest assetid
    var id_features = Filter($layer, "assetid IS NOT NULL")
    var max_id_feature = First(OrderBy(id_features, "assetid DESC"))

    // For an empty featureset, return the first assetid in the dataset
    if (max_id_feature == null) { return "MH.00001" }

    // Calculate and return the next assetid
    var max_id = max_id_feature.assetid
    var next_id_number = Number(Replace(max_id, "MH.", "")) + 1
    return "MH." + Text(next_id_number, "00000")
}

// If none of the above conditions are met, return null to do nothing
return $feature.assetid

 

UPDATE 2:

Slight modification to clean up the code and added elseif statements:

// Preserve the existing assetid if it already exists 

if (!IsEmpty($feature.assetid)) { 

    return $feature.assetid; 

} else if ($editcontext.editType == "UPDATE") { 

    // If the feature is being updated, explicitly return the existing assetid 

    return $feature.assetid; 

} else if ($editcontext.editType == "INSERT") { 

    // If this is a new feature (INSERT), calculate a new assetid 

    var id_features = Filter($layer, "assetid IS NOT NULL"); 

    var max_id_feature = First(OrderBy(id_features, "assetid DESC")); 

  

    // For an empty featureset, return the first assetid 

    if (max_id_feature == null) { 

        return "MH.00001"; 

    } 

  

    // Calculate and return the next assetid 

    var max_id = max_id_feature.assetid; 

    var next_id_number = Number(Replace(max_id, "MH.", "")) + 1; 

    return "MH." + Text(next_id_number, "00000"); 

} 

// Default behavior: return null to avoid overwriting the assetid 
return $feature.assetid; 

 

 

Still not working as expected.

 

 

0 Kudos
1 Solution

Accepted Solutions
HHight
by
Occasional Contributor

Figured out the solution.

Data had a couple dummy records with null asset ID values. Also modified the final return to be an else statement. This seemed to resolve the disappearing asset I'd. 

I'll leave this post up in case anyone else needs the code. 

View solution in original post

1 Reply
HHight
by
Occasional Contributor

Figured out the solution.

Data had a couple dummy records with null asset ID values. Also modified the final return to be an else statement. This seemed to resolve the disappearing asset I'd. 

I'll leave this post up in case anyone else needs the code.