Select to view content in your preferred language

Basic Arcade question - Show last inspection date from related record in pop-up

507
9
Jump to solution
08-25-2024 07:02 AM
elpinguino
Frequent Contributor

Hi Field Mappers,

Got an easy win for you.. I think? I am still fairly new to working with FeatureSets in arcade.

I have a parent/child relationship point and record. I want to show the last date checked from the record in the parent pop-up. When I go to add it in as an attribute expression and "Run" it it works, however when I go into map viewer and in Field Maps I can't get it to work.

I've followed a few basic tutorials online, but am not sure what's wrong here.

I have double and triple checked I have the correct FeatureSetByRelationshipName relationship name and checked field names for upper and lower cases.

My parent and child are both points.

var relatedrecords = OrderBy(FeatureSetByRelationshipName($feature,"BaitStation_Checks"), "DateChecked DES");
var cnt = Count(relatedrecords);
var relatedinfo = "";
if (cnt > 0) {
var info = First(relatedrecords);
relatedinfo = Text((info.DateChecked), "ddd DD/MM/YYYY");
}
DefaultValue(relatedinfo,"No checks to date")

 Any help you can offer would be greatly appreciated.

In addition I would like to grab info from two more fields in the most recent check. Same feature we're calling from above. If anyone has a tip for that I'd love to know. Thanks in advance for your time and helping me broaden my arcade knowledge.

0 Kudos
1 Solution

Accepted Solutions
ZachBodenner
MVP Regular Contributor

Yeah okay, so if you're using the Arcade Element in the popup, the return must be enclosed in those curly brackets. Essentially what you could have done to make your original code work here (as opposed to an attribute expression) is to do this:

return{
 type:'text'
 text: DefaultValue(relatedinfo, "No checks to date")
}

View solution in original post

9 Replies
ZachBodenner
MVP Regular Contributor

Try using "DESC" instead of "DES"? I don't know if that's what's going wrong, but I think Arcade using DESC from sorting.

0 Kudos
jcarlson
MVP Esteemed Contributor

Evidently "DES" is totally fine with Arcade!

jcarlson_0-1724677535777.png

It gets weirder and funnier. Literally any text after the field name besides "ASC" is treated as "DESC". A typo on "DESC" is fine. But a typo on "ASC" will give you the opposite!

jcarlson_1-1724677650890.png

jcarlson_2-1724677722381.png

 

- Josh Carlson
Kendall County GIS
0 Kudos
ZachBodenner
MVP Regular Contributor

That is crazy and funny, but I guess that's a built in null check, which has probably helped a lot of people out!

0 Kudos
jcarlson
MVP Esteemed Contributor

Honestly, I don't see anything wrong with your expression here, except maybe your OrderBy function needs a "C" on "DES"? Attribute rules are supposed to have access to all FeatureSet functions. You're saying that the expression doesn't work in the web map or Field Maps, just in the expression builder?

Two suggestions:

First, include more parameters on your FeatureSetByRelationshipName function. By specifying the fields you want and not including any geometry, less data will be going back and forth, which would speed up a slow expression.

Second, maybe just try some debugging. Add Console statements to your expression, then open your browser's Developer Tools and watch the "Console" tab and see what it says.

var relatedrecords = OrderBy(
  FeatureSetByRelationshipName(
    $feature,
    "BaitStation_Checks",
    ['DateChecked'],
    false
  ),
  "DateChecked DESC"
);

var cnt = Count(relatedrecords);
Console('related record count', cnt);
var relatedinfo = "";

if (cnt > 0) {
  var info = First(relatedrecords);
  Console(info)
  relatedinfo = Text((info.DateChecked), "ddd DD/MM/YYYY");
}

return DefaultValue(relatedinfo, "No checks to date")

 

jcarlson_0-1724677243860.png

 

- Josh Carlson
Kendall County GIS
0 Kudos
ZachBodenner
MVP Regular Contributor

Another thought is that there have been a lot of reports of FeaturesetByRelationshipName giving people trouble. You could try FeaturesetByName instead? 

Interesting not that FeaturesetByRelationshipName actually fixed a problem I had gotten when I was using FeaturesetByName, but hey, might be worth a short either way.

0 Kudos
elpinguino
Frequent Contributor

@ZachBodenner @jcarlson Thank you both for your replies!!!

I've learned a lot and feel like I'm armed well for future featureset arcade troubleshooting, and you've really challenged me to up my developer game 🤣 I had a long message written out to you, and then decided to test just one more thing, and well... that thing worked.

Short story

It works if I add it as an Attribute expression not as an Arcade element... maybe that was just a rookie mistake on my part. Why is that?

elpinguino_1-1724725424218.png

 

Long story

  • I changed DES to DESC.
  • I changed featuresetbyrelationshipname to featuresetbyid.

It is returning the latest bait station check date in the expression builder. Before it used to only return "No checks to date."

It doesn't show in the pop-up still though, and I've made sure I'm testing on a pop-up that has a bait station check.

I've had a look at the Developer Tools> Console. (Still new to this part, so tell me if I've missed something.)

It looks like it sees there are related records, and the dates for them.

elpinguino_0-1724725129132.png

This is my latest arcade expression.

//FeatureSetById(featureSetCollection_, id_)

var relatedrecords = OrderBy(
  FeatureSetById(
    $map,
    "19188cd37b9-layer-124",
    ['DateChecked'],
    false
  ),
  "DateChecked DESC"
);

var cnt = Count(relatedrecords);
Console('related record count', cnt);
var relatedinfo = "";

if (cnt > 0) {
  var info = First(relatedrecords);
  Console(info)
  relatedinfo = Text((info.DateChecked), "ddd DD/MM/YYYY");
}

return DefaultValue(relatedinfo, "No checks to date")

It still doesn't show in the pop-up though.

Next steps

Now I'm trying to get the featureset to pull three more fields and output that as "(BaitType) (BaitAdded) by (X) on ddd DD/MM/YYYY."

 

0 Kudos
ZachBodenner
MVP Regular Contributor

When you made the arcade element expression, did you remove the pre-filled return portion? The return needs to be in that format for the arcade element version to work, you can't just have a simple return.

0 Kudos
elpinguino
Frequent Contributor

Yes I removed this:

/*
Open the Suggestions tab and choose a template to get started creating different content types for your pop-up.
To learn more about using Arcade to create pop-up content visit:
*/

return {
  type : 'text',
  text : 'place your text or html here' //this property supports html tags
}
 
I don't understand enough about writing code to understand what you mean. Can you show me?
0 Kudos
ZachBodenner
MVP Regular Contributor

Yeah okay, so if you're using the Arcade Element in the popup, the return must be enclosed in those curly brackets. Essentially what you could have done to make your original code work here (as opposed to an attribute expression) is to do this:

return{
 type:'text'
 text: DefaultValue(relatedinfo, "No checks to date")
}