<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Arcade Auto Calculate AssetID for New Feature in ArcGIS Online Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-questions/arcade-auto-calculate-assetid-for-new-feature/m-p/1575677#M63165</link>
    <description>&lt;P&gt;Figured out the solution.&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll leave this post up in case anyone else needs the code.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Jan 2025 01:24:34 GMT</pubDate>
    <dc:creator>HHight</dc:creator>
    <dc:date>2025-01-15T01:24:34Z</dc:date>
    <item>
      <title>Arcade Auto Calculate AssetID for New Feature</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-auto-calculate-assetid-for-new-feature/m-p/1575597#M63161</link>
      <description>&lt;P&gt;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&lt;/P&gt;&lt;P&gt;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&amp;nbsp; have is when I go to edit a feature with an existing assetid, the assetid is wiped and set to a null.&lt;/P&gt;&lt;P&gt;My code is below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;// 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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I missing that would allow me to insert the next assetid but not touch or modify existing assetid values?&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;// 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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;UPDATE 2:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Slight modification to clean up the code and added elseif statements:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;// 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; 

 &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Still not working as expected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 22:10:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-auto-calculate-assetid-for-new-feature/m-p/1575597#M63161</guid>
      <dc:creator>HHight</dc:creator>
      <dc:date>2025-01-14T22:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade Auto Calculate AssetID for New Feature</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-auto-calculate-assetid-for-new-feature/m-p/1575677#M63165</link>
      <description>&lt;P&gt;Figured out the solution.&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll leave this post up in case anyone else needs the code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 01:24:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-auto-calculate-assetid-for-new-feature/m-p/1575677#M63165</guid>
      <dc:creator>HHight</dc:creator>
      <dc:date>2025-01-15T01:24:34Z</dc:date>
    </item>
  </channel>
</rss>

