GEOG 863:
Web Application Development for Geospatial Professionals

2.3.1 Getting the HMZ_ID


2.3.1 Getting the HMZ_ID

Recall that maintaining the relationship between the HMZ polygon feature and the Species_HMZ records associated with it depends on the GlobalID of the HMZ being entered into the Species_HMZ table’s HMZ_GUID field.  That was a result of the relationship class we created.  While this does maintain the record associations, the HMZ_GUID value (randomly generated by ArcGIS) will have no meaning to someone who views the Species_HMZ records in isolation.  That's what drove the inclusion of HMZ_ID in the Species_HMZ table; it’s an ID assigned by the DCNR staff.  However, as we saw, this field won’t be automatically populated for us.  So let’s see how we can use Arcade scripting to populate this field.

  1. In AGO, re-open your ISMP Web Map in the Field Maps Designer

  2. Return to the Form designer interface and re-open the Species HMZ table’s form. 

  3. Click on the HMZ_ID control to select it.  You should see its Properties appear in the panel to the right.

  4. Scrolling down that panel, you should see an area labeled Logic that has a Calculated expression property.  Click the gear icon to access the calculated expressions stored with the web map.  (There won't be any at this stage.) 

  5. Click New expression

    A dialog will open for building an Arcade expression, with a large box on the left for the expression and a collapsed panel on the right providing helpful shortcuts.  The shortcuts are in the form of Profile variables and Functions.

    The profile variables provide a number of potential starting points for expression building.  These include:
    $feature – the feature whose attributes the user is currently viewing (or as in this case, a record from a standalone table rather than a feature from a feature class)
    $map – the web map the user is currently viewing
    $layer – the layer/table that the current feature/record is coming from
    $featureSet – the complete set of features/records associated with the current layer; similar in concept to an array, common in programming languages; can be iterated over using a loop
    $datastore – the complete set of layers/tables that are built into the web map

    The Functions tab gives access to many general purpose functions found in many languages (e.g., Left, Right, Length, Count, etc.) along with spatial functions (Area, Centroid, Distance, Extent, etc.).

    In order to retrieve the HMZ_ID value, we need to get at the HMZ feature associated with the currently open Species_HMZ record.  We can do that by a) getting the HMZ_GUID, b) querying the HMZ layer for the HMZ feature with that value in its GlobalID field, and c) asking for the value in that feature’s HMZ_ID field.

  6. Start with step a) by entering the following:

    var hmz_guid = $feature["HMZ_GUID"]

    Note that:
    - The var keyword is used in Arcade to define variables.
    - Field values are accessed from features/records by quoting the field name and putting it in brackets.
    - You can expand the $feature profile variable to the right to see a list of the fields available and to insert the field reference automatically (by clicking the appropriate link).

    Let’s confirm that we’re on the right track before moving on to the next step. 

  7. Add the following statement to the expression:

  8. Click the Run button in the upper left of the Expression box. 

    Field Maps may churn for a few seconds, but eventually you should see a new section appear in the interface beneath the Expression box.  The Output tab will show the result returned by the expression – which we haven’t gotten to yet.  What we want to look at now is the Console tab, which is where the Console function sends its output.  Assuming all is well, you should see a GUID.  (Recalling that $feature refers to the record currently being viewed, you may be wondering which record it’s referring to when testing here in the expression builder.  I presume Field Maps is grabbing the first record it finds in the applicable table, Species_HMZ in this case.)

    Returning to the expression, we can now work on step b) of the steps outlined above.  Arcade has a FeatureSetByName function that can be used to get a reference to a FeatureSet (layer or table) through the global $map variable. 

  9. First, remove the Console statement. 

  10. Next, get a reference to the HMZ layer, as follows:

    var hmz_lyr = FeatureSetByName($map, "HMZ")

    Please note that your layer might not be called just "HMZ" it's likely called "ISMP Web Layer aaa123 - HMZ" so you'll need to amend your code accordingly. To find out what your layer is called open it in Map Viewer and use the name in the Layers list.
    Now we want to query the HMZ layer for all features where the GlobalID is equal to the value in our hmz_guid variable. 

  11. Carry out this operation using the Filter function:

    var qry = "GlobalID = '" + hmz_guid + "'"
    var feat_set = Filter(hmz_lyr, qry)

    Here we’re passing the Filter function a reference to the HMZ layer and a query expression.  Note that GUID values must be quoted, so in creating the qry variable, the concatenation operator (+) is used to enclose the GUID in single quotes.  When querying a layer in this way, the Filter function will return a FeatureSet object, which we’re storing here in the feat_set variable.

    In other contexts, we might use a for loop to iterate over the Features in a FeatureSet.  However, given the nature of GUIDs, we know that this FeatureSet will hold exactly 1 Feature. 

  12. Use the First function to get at the 1 returned Feature:

    var feat = First(feat_set)

    With a reference to the HMZ feature associated with the current Species_HMZ record, we’re ready to specify our Arcade expression’s return value (i.e., the value that should go into the HMZ_ID field).  This is done in Arcade using the return keyword followed by the desired value.

  13. Define the expression's return value, as follows:

    return feat["HMZ_ID"]

    Your complete Arcade expression should look like this:

    var hmz_guid = $feature["HMZ_GUID"]
    // remember that your layer might not be called "HMZ" so you 
    // might need to amend your layer name to be something like :
    // ISMP Web Layer aaa123 - HMZ
    var hmz_lyr = FeatureSetByName($map, "HMZ")
    var qry = "GlobalID = '" + hmz_guid + "'"
    var feat_set = Filter(hmz_lyr, qry)
    var feat = First(feat_set)
    return feat["HMZ_ID"]
  14. With that, you can again click Run

    As the expression contains no Console statements, you should expect to see nothing under the Console tab.  However, with the addition of a return statement, you should now expect to see something under the Output tab. Specifically, if you added a single Species_HMZ record associated with the Campground Management Area in your testing of the app at the end of the last section, you should see that the output returned by the expression is 12

  15. Before leaving the expression builder, set the Title of the expression to getHMZ_ID.

  16. Click Done to close the expression builder and return to the form designer. 

    Back in the form designer, you should now see a "Calculated" label beneath the HMZ_ID field and your getHMZ_ID expression listed under the Calculated value area of the Properties panel.

  17. Click the Save button in the upper right of the form designer to commit your changes.

  18. At this point, you can return to Field Maps on your mobile device to test the change you just made.  If you’re looking at the list of Maps available, you’ll probably see your ISMP Web Map listed under the Current heading.  It’s probably a good idea to tap the button next to it and then Reload Map to ensure the changes have made their way to your device.

  19. Open the map and open the popup for the Campground Management Area again. 

  20. Access the related Species_HMZ form and click Add.  The form associated with that table should appear and instead of defaulting to 0, the HMZ_ID should be set to 12.  (It may take a second or two for the calculation to complete.)  There’s no need to submit another species at this point, so you can tap Cancel and then Discard to back your way out of adding a new species.

That wasn’t a mission-critical calculation, but it did give a good introduction to writing calculated expressions with Arcade.  You can learn more about the topic from the Field Maps documentation (  In the next section, we'll build the expressions that are needed to compute the priority score for each Species_HMZ record.