Select to view content in your preferred language

Autopopulating fields from a related table using Arcade

2192
14
04-11-2023 07:39 AM
DaveK
by
Occasional Contributor

Hello! 

I'm creating a Field Maps collection form which includes a parent point layer as well as a related table. The parent point layer contains a UniqueID field which is a domain that the user will select. Is it possible to auto populate the remaining fields in the parent table from the information in the related table based on the UniqueID chosen? I think the "Fetch an attribute form a related table" from this page - https://www.esri.com/arcgis-blog/products/field-maps/field-mobility/common-calculated-expressions-fo... is the closest to what I'm looking for. Instead of populating the related table from the parent, I would like to auto populate the parent layer from the related table. Any help is appreciated. 

Thanks. 

14 Replies
DougBrowning
MVP Esteemed Contributor

What you want is the FeatureSet function like this

var tbl = FeatureSetByRelationshipName($feature, "Points", ['DesignLat'], false);
return First(tbl).DesignLat

Hope that helps

0 Kudos
DaveK
by
Occasional Contributor

Thanks @DougBrowning

This is what I have so far. I ended up using FeatureSetByName. 

 

 

var related = FeatureSetByName($map, "pointlayer - points");
var uniqueid = $feature.uniqueid;
var relatedRecord = null;
if (uniqueid == '111111') {
relatedRecord = First(Filter(related, "uniqueid = '111111'"));
if (relatedRecord != null) {
return relatedRecord['Field1'];
}
} else if (uniqueid == '222222') {
relatedRecord = First(Filter(related, "uniqueid = '222222'"));
if (relatedRecord != null) {
return relatedRecord['Field1'];
}
}
return null;

 

 

 So far this works and it auto populates the information from the related table Field1 into Field1 in the parent layer. I cant figure out how to return other fields at the same time. For example, I have the Field1, Field2, Field3, etc, that I would like to auto populate into the parent layer. Do you have any ideas as to how I can include these as well? 

Thanks. 

0 Kudos
DougBrowning
MVP Esteemed Contributor

No you would need to do this for each field.  I have upwards of 32 to populate all of mine.  Well you could return them but you cannot store all that in one field so not sure what you mean.

Add the specific field you want and add the false part to not return the geometry and it will be faster like in my example.  I am not sure why you are hard coding in the 1111 thing vs just calling by name but I guess you have a reason.

0 Kudos
KenBuja
MVP Esteemed Contributor

You can simplify this by using the UniqueID directly in your filter

var related = FeatureSetByName($map, "pointlayer - points");
var uid = $feature.uniqueid;
var relatedRecord = null;
relatedRecord = First(Filter(related, "uniqueid = @uid"));
if (relatedRecord != null) return relatedRecord['Field1'];
return null;
DaveK
by
Occasional Contributor

Thanks @KenBuja this helped!

0 Kudos
ErikWalling
New Contributor II

Hi Doug, 

I have a very similar issue but just different enough so that the solution provided isn't quite the fix I need. I am trying to calculate all the values in a field based on the values from another field in a related table. Here's the set-up. The parent table Sewer_Lines contains PipeLengths for all of our sewer lines. We have a related table for flushing these lines that records each time the line is cleaned which also contains PipeLengh. Since we've already been keeping a flushing record for some time now, I'd like to have the PipeLength(s) from Sewer_Lines calculated into PipeLength(s) in the Sewer_Lines_Flushings table.  I am able to get the values this way

var flushlen = FeatureSetByName($datastore, "Sewer_Lines", ['PipeLength'])
flushlen

But this also returns the OBJECTID field, not just PipeLength. I've also tried this  

var flushlen = FeatureSetByName($datastore,"Sewer_Lines", ['PipeLength'])
var feature =First(flushlen)
if (!IsEmpty(feature)) {
return feature['PipeLength']
} else {
return 0
}

But it returns just the first record in the parent table and not all of the records. Is there a way to update all of the records in Sewer_Lines_Flushings.PipeLength based on Sewer_Lines.PipeLength? 

 

 

 

0 Kudos
DougBrowning
MVP Esteemed Contributor

Not sure what you mean by all the values into a field.  You can't have multiple values in one field.  

If you mean just create a string of values from the repeats then you do that by making a string in a loop like shown in this post.

https://community.esri.com/t5/arcgis-pro-questions/arcade-array-output-from-related-table-help/td-p/...

If you just want to display the repeat values then the new map viewer can do this for up to 10 records.

I think there is a way to display a table also but I forget.

Hope that helps

0 Kudos
ErikWalling
New Contributor II

Hi again,

I am trying to get this to work and have tried many variations without success, do you have any suggestions?

var x = ($feature.Scum + $feature.Sludge)
var y = FeatureSetByRelationshipName($feature,"MPCVRS", ['LiquidDepth'], False)
var z =First(y)
if (!IsEmpty(z)) {
    return 1
} else {
    return (x / z['LiquidDepth']) 
}
 
I am trying this through a form in ArcGIS online and then going to FieldMaps to test. I am trying to calculate  capacity (sludge depth + scum depth / liquid depth). My result for var x is what I expect but z seems to want to bring along object ID and not just the value for liquid depth. The other potential issue is scum and sludge depth is being provided by user input in the form but the form seems to run the calculation as soon as the form is opened rather than after user input.... Is there a way to change that or some other work around you're aware of? I should note that capacity does not need to be calculated in the form necessarily. The other way I could do this is in the main table and possibly in the pop-up but I am not sure if that type of expression would be allowed in the pop-up or if that's more for formatting text....  
DougBrowning
MVP Esteemed Contributor

I your First you need to give a field name.   Like this

var tbl = FeatureSetByRelationshipName($feature, "Points", ['DesignLat'], false);
return First(tbl).DesignLat

Also First will fail if it is null so if I remember right you need to something like this.

var x = ($feature.Scum + $feature.Sludge)
var y = FeatureSetByRelationshipName($feature,"MPCVRS", ['LiquidDepth'], False)
if (Count(y) > 0) {
        return x / First(y).LiquidDepth
}
else {
    return 1
}
 
Syntax may be off but you get the idea I hope.