ArcGIS Online is a powerful platform for creating and sharing maps and spatial data. One of its many features is the ability to relate data between tables within a feature layer. Using an arcade script, you can pull related records from a related table and display them in your webmap or web app. This is useful for showing additional information about a feature, such as inspection or maintenance history, ownership information, or any other data that may be associated with the feature.

Here are a few examples of how you can use arcade scripts to pull and display related data:

Displaying the Most Recent Inspection Date for a Building

This example shows how to display the most recent inspection date for a building. The script pulls all related records from the “Inspections” table, sorts them in descending order based on the “InspectionDate” field, and then displays the most recent inspection date.

var pulledrecords = FeatureSetByRelationshipName($feature, "Inspections", ['*'], false);
var relatedrecords = OrderBy(pulledrecords, "InspectionDate DESC");
var cnt = Count(relatedrecords);

var relatedinfo = "";
if (cnt > 0) {
    relatedinfo = Text(ToLocal(relatedrecords[0].InspectionDate), "MM/DD/Y");
}

return relatedinfo;

Displaying a List of Permits Associated with a Property

This example shows how to display a list of all permits associated with a property. The script pulls all related records from the “Permits” table, sorts them in ascending order based on the “PermitNumber” field, and then loops through the related records to display a list of permits.

var pulledrecords = FeatureSetByRelationshipName($feature, "Permits", ['*'], false);
var relatedrecords = OrderBy(pulledrecords, "PermitNumber ASC");
var cnt = Count(relatedrecords);

var relatedinfo = "";
if (cnt > 0) {
    for (var relatedrecord in relatedrecords) {
        if (relatedinfo == "") {
            relatedinfo = relatedrecord.PermitNumber + " - " + relatedrecord.Description;
        } else {
            relatedinfo += TextFormatting.NewLine + relatedrecord.PermitNumber + " - " + relatedrecord.Description;
        }
    }
}

return relatedinfo;

Sorting Related Records by Date

This example shows how to sort related records by date. The script pulls all related records from the “SampleData” table, sorts them in descending order based on the “CreationDate” field, and then loops through the related records to display them.

var pulledrecords = FeatureSetByRelationshipName($feature, "SampleData", ['*'], false);
var relatedrecords = OrderBy(pulledrecords, "CreationDate DESC");
var cnt = Count(relatedrecords);

var relatedinfo = "";
if (cnt > 0) {
    for (var relatedrecord in relatedrecords) {
        if (relatedinfo == "") {
            relatedinfo = relatedrecord.DO + " - " + Text(ToLocal(relatedrecord.CreationDate), "ddd MM/DD/Y @ h:m") ;
        } else {
            relatedinfo += TextFormatting.NewLine + relatedrecord.DO + " - " + Text(ToLocal(relatedrecord.CreationDate), "ddd MM/DD/Y @ h:m");
        }
    }
}

return relatedinfo;

As you can see, the possibilities are endless when it comes to displaying related data in your webmap or web app. By using arcade scripts to pull and sort related records, you can provide users with valuable information that may not be immediately visible in the map.

To use these scripts in your own webmap or web app, simply create a new arcade expression and copy and paste the code into the expression editor. Be sure to update the relationship name and field names to match your own data. Once you’ve created your expression, you can use it to display related data in a pop-up or custom widget in your webmap or web app. By leveraging the power of arcade scripts, you can create more informative and engaging maps and apps that provide users with a richer understanding of your data.