Select to view content in your preferred language

Arcade - FeatureSetByRelationshipName

3460
10
Jump to solution
08-15-2022 01:26 PM
by Anonymous User
Not applicable

Hello,  I am trying to populate a field (Site_ID) in a related table using the arcade function FeatureSetByRelationshipName.  The related feature that I am trying to pull from is called MTH_Campgrounds.

In Field Maps I want to select a point (campground in this example case) and select the related table icon and enter data into the form I created for the table.  Hopefully, this code will reduce the potential for error while inputting data into the table's form.  The feature and table are related using Global IDs.  The field in the table will be null at first.. so I am assuming there is an if then--first kind of arcade statement.  This is where I currently am...

var related = FeatureSetByRelationshipName($feature,'MTH_Campgrounds', ["SITE_ID"], false)

return related

 

Thanks in advance!

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Got the code to work, someone suggesting I don't use the FeatureSetByRelationshipName because it has some bugs, so I tried this and it works!  Thanks for all your help @MikeMillerGIS.  I was curious though what if the field site ID wasn't numeric, what if it was text would you change anything?

Here is the functioning code:

var related = FeatureSetByName($datastore,"MTH_Campgrounds", ['SITE_ID'], false)

var record = First(related)

if (IsEmpty(record)){

return $feature.TestID
}
return record['SITE_ID']

View solution in original post

0 Kudos
10 Replies
MikeMillerGIS
Esri Frequent Contributor

The result is a featureset, so you can loop over it or call first on it, such as :

var related = FeatureSetByRelationshipName($feature,'MTH_Campgrounds', ["SITE_ID"], false)

return First(related)['SITE_ID']

0 Kudos
by Anonymous User
Not applicable

Thank you for the response!  When I use that code I get this error response

Execution Error:Cannot read properties of null (reading 'toString')

 

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

Might need to add some error handling.

var related = FeatureSetByRelationshipName($feature,'MTH_Campgrounds', ["SITE_ID"], false)

var record = First(related)

if (IsEmpty(record)){

  return "No Record Found"

}

return record['SITE_ID']

by Anonymous User
Not applicable

So I learned that the FeatureSetByRelationshipName references the relationship class which I titled Evaluations.  So I enter that and the test runs!  But when I open the field maps mobile application it says calculation failed.  Any ideas?

var related = FeatureSetByRelationshipName($feature, "Evaluations", ['SITE_ID'], false)

var record = First(related)

if (IsEmpty(record)){

return "No Record Found"

}
return record['SITE_ID']

 

Thanks in advance!!!!

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

What type of field is SITE_ID, if it is numeric, that makes sense:

 

var related = FeatureSetByRelationshipName($feature, "Evaluations", ['SITE_ID'], false)

var record = First(related)

if (IsEmpty(record)){

return <ADD THE ASSIGNED TO FIELD HERE, SUCH AS $feature.THEFIELDTHISISASSIGNEDTO>

}
return record['SITE_ID']

by Anonymous User
Not applicable

Got the code to work, someone suggesting I don't use the FeatureSetByRelationshipName because it has some bugs, so I tried this and it works!  Thanks for all your help @MikeMillerGIS.  I was curious though what if the field site ID wasn't numeric, what if it was text would you change anything?

Here is the functioning code:

var related = FeatureSetByName($datastore,"MTH_Campgrounds", ['SITE_ID'], false)

var record = First(related)

if (IsEmpty(record)){

return $feature.TestID
}
return record['SITE_ID']

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

That code is incorrect. That will always return the first record in that table. You need to use filter and build a sql clause to get the record that matches $feature

0 Kudos
MikeMillerGIS
Esri Frequent Contributor
0 Kudos
JohnWatson_EBA
Regular Contributor

@MikeMillerGIS I added an attribute rule (Pro 2.9.5) to populate a field in the FC based on the value entered in the same field in the related table, on insert. Odd thing is when I test it and add a new related record to the FC, the field in the related record gets auto populated with the globalID, so I am not sure what that's about. And if I just manually edit that field to test the actual rule, the field does not populate in the feature class after the related record is added and saved.

// This rule will update an attribute in the parent feature

// Store the parent feature global from the key field in the relationship
var parent_id = $feature.GlobalID;
if (IsEmpty(parent_id))
    return parent_id;

// force to upper as the sql is case sensitive
parent_id = Upper(parent_id);

// Using the GDB name, get the parent classes records and Global ID field
var parent_class = FeatureSetByName($datastore, "Utilities_Model.GISADMIN.ssManhole", ["globalid", 'Lid_Condition'], false);
// Filter the parent class for only related features
var parent_records = Filter(parent_class, "globalid = @parent_id");

var updates = [];
var i = 0;
var new_value = 'Lid_Condition';
 
if (IsEmpty($feature.Lid_Condition) == False)
{
    new_value = $feature.Lid_Condition
 
}       

// Loop through each  feature, create a dict of the Global ID and the new value date
for (var row in parent_records) {
    // If the parent row is null or has a different value, updated it
    if (IsEmpty(row['Lid_Condition']) || row['Lid_Condition'] != new_value)
    {
        updates[i++] = {
            'globalid': parent_id,
            'attributes': {"Lid_Condition": new_value}    
        };
    }
}

// Return the original value in the result parameter, as to not lost the entered value
// Using the edit parameter,  return the class and list of updates
return {
'result': parent_id,
'edit': [
            {'className': 'Utilities_Model.GISADMIN.ssManhole',
             'updates': updates
            } 
        ]
};

 

0 Kudos