Creating a Arcade expression

190
1
02-23-2024 08:28 AM
Labels (2)
ColeShelley
New Contributor

Currently, I am trying to write an Arcade expression. I have one feature layer that is related to three standalone tables. In this scenario, I am only using the feature layer "Layer" and one of the tables called "status". This feature layer is used in ArcGIS Field Maps where our construction and engineering teams can use and update project development while out in the field. When they update the status field in the status table, I want that to automatically update the status field in the feature layer. These two are related through the GlobalID in the status standalone table and the status_ID (which is a GUID) in the feature layer. Below is my script I created. I have tried so many different ways and I keep getting errors. I created a script in Python that works on Pro but I need something in Arcade that works on Map Viewer so I can configure the pop up. Here is my current Arcade script:

var layer = FeatureSetById('cc74c700497f4e64bbf9ffe157108c90', ["status", "GlobalID"]);
var table = FeatureSetById('cc74c700497f4e64bbf9ffe157108c90', ["status", "status_ID"]);

var updatedStatus = "No Match";

// Loop through features in the layer
for (var feature in layer.features) {
      var layerGlobalID = feature.attributes.GlobalID;

      // Find corresponding record in the table
      var matchingRecords = Filter(table, 'status_ID = @layerGlobalID');

      if (Count(matchingRecords) > 0) {
           // Get the status value from the matching record
           var matchingStatus = First(matchingRecords).attributes.status;
          // Update the status field in the layer feature
           feature.attributes.status = matchingStatus;
           updatedStatus = matchingStatus;
      }
}

return updatedStatus

0 Kudos
1 Reply
KenBuja
MVP Esteemed Contributor

You're supplying the FeatureSetById function incorrect parameters, which would look like this:

var features = FeatureSetById($map,'DemoLayerWM_1117', ['*'], true);

It looks likes you should be using the FeatureSetByPortalItem, which looks like this:

var features = FeatureSetByPortalItem(
  Portal('https://www.arcgis.com'),
  '7b1fb95ab77f40bf8aa09c8b59045449',
  0,
  ['Name', 'Count'],
  false
);

If you have set up a relationship between the feature layer and the tables you can use the FeatureSetByRelationshipName function. This allows you to get there related records without having to loop through all the features.

Here's an example of retrieving the related records for a popup.

var rec = First(FeatureSetByRelationshipName($feature, "DCGIS.ITSPE", ['BIDNAME', 'BIDTOTALDUE', 'BIDCOLLECTED', 'BIDBALANCE']));
if (IsEmpty(rec.BIDNAME)) return 'Not a BID';
return `${rec.BIDNAME}
    • Total Due: ${Text(rec.BIDTOTALDUE, '$#,###.00')}
    • Collected: ${Text(rec.BIDCOLLECTED, '$#,###.00')}
    • Balance: ${Text(rec.BIDBALANCE, '$#,###.00')}`

This example is uses a public service, so you can examine how it uses the relationships.

related.png