Showing Field from Related Record in Pop-up Using Arcade FeatureSetByRelationshipName

919
3
Jump to solution
03-17-2022 02:51 PM
Billy
by
Occasional Contributor II

Hi,

My system configuration:

Arcgis Enterprise: 10.8.1

Arcgis Pro: 2.9.2

I'm getting an "Error on line 6. Unexpected null" when I try to validate the code shown below in the Pop-up profile of Pro. The code tries to get a record from a related table of a feature and show one field of the related record in the Pop-up. I'm using FeatureSetByRelationshipName and Filter functions to get the records from the related table.

I tried with different field names or the 2 syntax (feature.fieldname or feature['fieldname'], and I always get this "Unexpected null" error when trying to query a field. I know that everything is working until line 6 because I used the Count function and it returns the expected number of record count; also, I used the TypeOf function to check the type of the variable "t" and it returned "Feature".

 

var results_FS = FeatureSetByRelationshipName($feature, 'R0L100Electric_DeviceL1508Device_Unit', ['*'], false)
var transf_FS = Filter(results_FS, "assettype = 785")

var t = First(transf_FS)

if(IsEmpty(t.assetid)){
     return domainName($feature, 'assettype') + ': ' + $feature.assetid
} else {
    var s = t.assetid
    domainName($feature, 'assettype') + ': ' + $feature.assetid + ': ' + s

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

If you call First() on an empty feature set, it will return null. So when you then call null.assetid, this will fail.

As Kim said, you have to check if transf_FS contains any features. You could do that by using Count(), but that is expensive for large feature sets. It's easier to check if t is null:

var t = First(transf_FS)
if(t == null) {
  return "Some default value"
}
// continue with your code
if(IsEmpty(t.assetid)){
  ...

Have a great day!
Johannes

View solution in original post

0 Kudos
3 Replies
KimGarbade
Occasional Contributor III

Have you tried seeing if your "transf_FS" feature set has any records in it?  Something like:

var transf_FSCount = Count(transf_FS)
  if (transf_FSCount > 0){
      if(IsEmpty(t.assetid)){
          return domainName...and the rest of your code here
          .
          .
      }
  }
 
I'm thinking Arcade might be returning t as a type "Feature" because the First function returns a Feature if a FeatureSet is used as input into the function... regardless of if that chunk of memory defined as t (a Feature) holds any actual data or not.
0 Kudos
JohannesLindner
MVP Frequent Contributor

If you call First() on an empty feature set, it will return null. So when you then call null.assetid, this will fail.

As Kim said, you have to check if transf_FS contains any features. You could do that by using Count(), but that is expensive for large feature sets. It's easier to check if t is null:

var t = First(transf_FS)
if(t == null) {
  return "Some default value"
}
// continue with your code
if(IsEmpty(t.assetid)){
  ...

Have a great day!
Johannes
0 Kudos
Billy
by
Occasional Contributor II

Hi,

I confirmed that the FeatureSet returned by the FeatureSetByRelationshipName was not empty with a count and also used an If statement to check if "t.assetid" was "Null", before posting the question.

You can see below the and image showing the the FeatureSets are not empty, before and after the filter function.

var results_FS = FeatureSetByRelationshipName($feature, 'R0L100Electric_DeviceL1508Device_Unit', ['*'], false)

var fs_cnt = Count(results_FS)

var transf_FS = Filter(results_FS, "assettype = 785")

var transf_fs_cnt = Count(transf_FS)

var t = First(transf_FS)

var s = TypeOf(t)

domainName($feature, 'assettype') + ': ' + $feature.assetid + ': ' + '/ FeatureSet Count:' + fs_cnt + ' / Transf FeatureSet Count: ' + transf_fs_cnt + ' / Object Type: ' +  s

Billy_0-1647606160831.png

 

Now, after adding the check if the Feature is "Null" then the code passes the validation and also returns the fields from the related record. It looks like the code validation requires handling the exception of possible "Null" feature before being able to access the fields. As you can see in the image below the code is able to get the fields and show them in the Pop-up, so it was never "Null".

 

var results_FS = FeatureSetByRelationshipName($feature, 'R0L100Electric_DeviceL1508Device_Unit', ['*'], false)
var transf_FS = Filter(results_FS, "assettype = 785")

var t = First(transf_FS)

if(t == null) {
        return "Some default value"
      }  

if(IsEmpty(t.assetid)){
     return domainName($feature, 'assettype') + ': ' + $feature.assetid
} else {
    var id = t.assetid
    var power = t.powerrating/1000 
    domainName($feature, 'assettype') + ': ' + $feature.assetid + ': ' + id + ' (' + power + ')'
}

 

Billy_1-1647607658013.png

 

0 Kudos