Select to view content in your preferred language

How do you reference another field from a related table in Arcade?

20102
61
02-26-2019 06:19 AM
deleted-user-TRj-mR_qEmwV
New Contributor III

Through a Web Map in ArcGIS Online, I'm creating a field in Feature #1 and Feature #1 is related to Feature #2.  Is there a way to get the specific values in Feature #2 that are related via for an Arcade expression in Feature #1?

It's a 1 to many relate and related through a Text ID Field that they both have. 
EDIT: I think it has something to do with FeatureSetBy functions

0 Kudos
61 Replies
XanderBakker
Esri Esteemed Contributor

Hi joe rodmey ,

Since you shared that code I assumed that you had the first part working already, but I see now that it is an exact copy of what I posted earlier in this thread. This mean that we should go back to the beginning and determine your exact data structure and fields that are related. In case you work with Global IDs your SQL query for the Filter will change too.

So, what is the attribute from the feature that is used to establish the relation with the related table? What is the name of the attribute it is related to in the related table? 

0 Kudos
joerodmey
MVP Alum

Hi Xander Bakker

The related table was created through the Survey123 repeat type. It appears that it uses parentglobalid as the relation. This relates back to the global ID in the parent.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi joe rodmey , 

Would it be possible to share the survey form with me? Perhaps create a group, share your survey in that group and invite me using "xbakker@esri.co_utility_esri_co"?

0 Kudos
joerodmey
MVP Alum

To begin with I can provide screenshots/info you need

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Joerodney ,

OK, let's start with that. 

0 Kudos
joerodmey
MVP Alum

Here is what the repeat looks like in Survey123:

And here this sample you can see the relationship field of parent global id found in the related table REST page:

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Joerodney 

Could you try this?

// first read out the GlobalID of the Feature
var code = $feature["globalid"];

// create a sql expression to query on GlobalID
var sql = "parentglobalid = '{" + Upper(code) + "}'";
Console(sql);

// access the table
var tbl = FeatureSetByName($datastore, "Work");

// filter the table using the sql expression
var related_data = Filter(tbl, sql);

// count the resulting records
var cnt = Count(related_data);
Console(cnt);

// initiate a variable to hold the result
var result = 0;

// check if there are related records found for the current ID
if (cnt > 0) {
    // loop through related records
    for (var row in related_data) {
        // add the value  to the result
        result += row.item; 
    }
}

// return the result
return result * 2.5;

Notice that the sql is formed differently due to the use of GlobalIDs.

joerodmey
MVP Alum

Thanks so much Xander! It works now. Will continue to test but looks very promising. 

XanderBakker
Esri Esteemed Contributor

Hi joe rodmey , I'm glad to hear that! Keep me posted and if you have any additional questions, please let me know. 

0 Kudos
joerodmey
MVP Alum

Hi Xander Bakker‌ 

I'm also looking for a way to fill in a field (in the same related table) with "NA" if there is no value in this particular

Any ideas?

Thanks

0 Kudos