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
Solved! Go to Solution.
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 you tried seeing if your "transf_FS" feature set has any records in it? Something like:
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)){
...
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
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 + ')'
}