Select to view content in your preferred language

Arcade error when accessing empty feature set

8939
16
Jump to solution
04-26-2020 08:51 AM
DonMorrison1
Occasional Contributor III

I'm just starting to use Arcade - like it a lot so far but there is one scenario that I can't figure out. I want to retrieve the set of related records associated with a feature for my ArcGIS Online web map popup. I can do this with this statement

var fs = FeatureSetByRelationshipName($feature, "Program", ["ProgramName"], false);

It works great except when the feature has no related records. I still get a FeatureSet returned, and I would expect it to contain zero features. But any method I use to access the FeatureSet returns "Execution Error:Cannot read property 'toString' of null". I've tried all of the following:

  • Count(fs)
  • for (var f in fs) {}
  • First(fs)

What is the trick to allow me to detect in my code that the feature set is empty?  

16 Replies
KARIMLABIDI
Occasional Contributor

Hello Everybody,

I have the same problem than DonMorrison1 but I can't solved it. I got the same error msg.

KARIMLABIDI_1-1647618557004.png

KARIMLABIDI_2-1647618594559.png

I tried the little code of Dan but it doesn't work .

Any idea?

Thanks a lot

 

 

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Your "build out popup" code is wrong.

If you only want to return the first entry of relateData:

var firstRelateData = First(relateData)
if(firstRelateData == null) {  // no related data
  return "null"
}
return firstRelateData.num_arret

 

If you want to return all related features:

if(Count(relateData) == 0) {  // no related data
  return "null"
}
var all_num_arrets  = []
for(var f in relateData) {
  if(f.num_arret != null) {
    Push(all_num_arrets, f.num_arret)
  }
}
return Concatenate(all_num_arrets, ", ")

Have a great day!
Johannes
0 Kudos
KARIMLABIDI
Occasional Contributor

Thank you Johannes but I Got the same problem. I can't count the relateData with the filter, the problem is in. 

I don't understand this error msg because I have data in each row of my table.

KARIMLABIDI_0-1647850546749.png

KARIMLABIDI_1-1647850712363.png

 

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Hmmm. Try adding this at the start of your script:

if(IsEmpty($feature.IDARRET)) {
  return "null"
}

 


Have a great day!
Johannes
0 Kudos
KARIMLABIDI
Occasional Contributor

yes it works. Thank you!!

Juste one more question about it: do U know how can i put a little symbol just before my attributes. When i do like this 

for(var f in relateData) {
  if(f.num_arret != null) {
    Push(all_num_arrets, 
    ` "https://xxxxxxxx/rest/content/items/9e3929af55db440ea45c5f72e37cd3c5/data" ${f.hor_theo_1mn} `)
    Push(all_num_arrets, 
    ` "https://xxxxxxxx/rest/content/items/9e3929af55db440ea45c5f72e37cd3c5/data"${f.hor_theo_2mns} `)
  }
}

, it just gives me the URL and not the image. 

Thank you

Tags (1)
0 Kudos
JohannesLindner
MVP Frequent Contributor

Arcade doesn't evaluate HTML. Maybe what you want to do is possible, but it will be a little complicated.

I suggest posting a new question for this, as this goes far beyond the scope of the original question, and you'll get more views and potential answers.


Have a great day!
Johannes
Tags (1)
0 Kudos
ZachBodenner
MVP Regular Contributor

Hello, I'm getting an issue trying to run this procedure but I can't quite figure out what's wrong. Here's my code:

var assets = FeatureSetByRelationshipName($feature, 'epgdb.PARKS.Asset_Inventory', ['*'], true)
console(assets)
console(count(assets))
console(typeof(assets))
// Get the first park asset (should only be one)
var asset = First(assets)
Expects(assets, 'AssetType')
// If there was an asset, return the AssetType of it,
// Otherwise, return null
if (!IsEmpty(asset)) {
    return asset['AssetType']
} else {
    return null
}

 I have park assets, with a related inspection table. The table is where I'm placing this code, and the relationship name in the featureSetByRelationshipName function is the backward path label from my EGDB relationship class (is that correct?)

ZachBodenner_0-1692809664418.png

I've tried adjusting a lot of the parameters but the closest I get is always returning null:

ZachBodenner_1-1692809719293.png

 

The console messages indicate 0 assets, which I guess makes sense considering this is just in the expression builder and probably doesn't know what the related table would be targetting?

ZachBodenner_2-1692809788307.png

 

The problem is that when I open Field Maps and try to create a related record, the field I add the calculation to just gives me the error: "Failed to calculate"