Arcade: Can you reference an existing expression in another expression?

1042
8
10-25-2019 09:56 AM
RobBlash
Regular Contributor

Let's say I have {expression/expr0} written, and I want to write a new expression to use the same output but scrubbed a different way. Can I reference $feature.{expression/expr0}? That syntax doesnt work, but hopefully this is possible and I'm doing it wrong?

Tags (1)
8 Replies
Peter_Klingman
Esri Regular Contributor

Hi Rob,

You can reference existing expressions in the Existing tab of the expression authoring window:

Hope that helps,

-Peter

NicholeSalinger
New Contributor III

Hey Peter Klingman‌ - is there a way to just embed the name of the expression? I'm pretty new at using Arcade Expressions and the online documentation doesn't reference how to embed expressions in another expression.Thank you!

Peter_Klingman
Esri Regular Contributor

Hi Nichole, it is not currently possible to reference the expression by name. You would need to select the existing expression from the right hand menu, and this will insert it into the expression that is currently being authored. For all intents and purposes it is the same as reusing the expression by name, albeit makes the script longer. Hope this helps!

-Peter 

NicholeSalinger
New Contributor III

Hey Peter - I ended up figuring out the expression because the embedded code would not cooperate using the method you described. Thanks though!

KeithBurdette1
New Contributor III

You say that "For all intents and purposes it is the same as using the expression by name" 

I'm using arcade to reference a related table to return a few attributes.  By retyping the code instead of referencing an earlier expression, that means I need to do a separate function call for FeatureSetByName and Filter for each different attribute I want to return.  For ten attributes, that means ten different returns of my entire dataset which isn't very fast.  If I could reference earlier expressions, I could set one of them to the returned featureset that has been filtered and not need to make a new featureset for each attribute.  Is storing a featureset in an expression a possibility in arcade?

0 Kudos
Peter_Klingman
Esri Regular Contributor

Hi @KeithBurdette1 - what I mean is that inserting the lines of an existing expression into a new expression will execute the same code as if you were importing/referencing the existing expression. Since Arcade is data-driven and looks at your feature layers, there isn't really the concept of storing a FeatureSet in memory and using it in a different expression without making the call to go and get the features again. In your case, can you make one chained Filter/FeatureSetByName call and return all 10 attributes you need to a single FeatureSet? Then you can use dot or bracket notation to access the attributes you need out of the FeatureSet, which would be stored as a variable available within the scope of that expression. I believe this would only make one server call instead of 10. If you want to post your code here I'm happy to take a look as well. 

Thanks,

-Peter

0 Kudos
KeithBurdette1
New Contributor III

Thanks for the reply Peter!

Ultimately, if I want to display all the attributes returned by a FeatureSet, don't I need to have an expression for each individual attribute?  Or by chain, do you mean a string that displays all the attributes I want that I could then put in a pop-up?

0 Kudos
Peter_Klingman
Esri Regular Contributor

Hi @KeithBurdette1 - By chain I meant use Filter and FeatureSet in the same call to reduce round trips to the server. But you're spot on - to return multiple attributes from the related records, what you can do is create an empty string, loop through the featureSet, and add records to the string. Here is a quick example of both:

var globalId = $feature.GlobalID;
var fset = Filter(FeatureSetByName($map,"RelatedFeatures"), 'RELATEFIELD = @globalId');
var returnString = "";
for (var f in fset) {
    returnString += "Related Record # " + f.NUMBER_RELATED
    returnString += TextFormatting.NewLine
    returnString += f.TEXT_RELATED
    returnString += TextFormatting.NewLine
    returnString += "-----------------"
    returnString += TextFormatting.NewLine
}

return returnString

 

Returns:

Peter_Klingman_0-1627509071580.png

 

Thanks,

-Peter

0 Kudos