Select to view content in your preferred language

Dealing with an Arcade Expression that Fails Under Certain Circumstances

384
3
03-25-2024 10:02 AM
StephenEJohnson
New Contributor

I am calculating a field in Field Maps by checking data in a table related to the layer. The problem is that if there are no related values in the table the expression evaluates to an error which prevents the data from being submitted. What I want to do is assign a string value of 00 to the field if there are no records in the related table but my code fails instead of assigning that value. I think what is happening is that the FeatureSetByRelationshipName function returns an error if there aren't related records and the part of my function that returns 00 is not evaluated. There isn't, as far as I know, an on error type function in Arcade that I could use to check if the function FeatureSetByRelationshipName evaluates to an error. If there was it would be a simple matter of checking that then assigning 00 if there is an error. The !IsEmpty in my code doesn't work because the function doesn't return null for the variable it returns an error. Does anyone know how I can get around this?

 

var relatedFeatures = FeatureSetByRelationshipName($feature, "RecordType2", ["Rep"], false);

if (!IsEmpty(relatedFeatures) && $feature.EventType == 'Sampling') {
    return text(count(relatedFeatures),"00");
} else {
    return '00';
}
0 Kudos
3 Replies
DougBrowning
MVP Esteemed Contributor

I think I have got this to work before.

I wonder if it is doing the && first.  Maybe try

var relatedFeatures = FeatureSetByRelationshipName($feature"RecordType2", ["Rep"], false);

 

if (!(IsEmpty(relatedFeatures)) && ($feature.EventType == 'Sampling')) {
    return text(count(relatedFeatures),"00");
else {
    return '00';
}
 
I have also used count in the past
var relatedFeatures = FeatureSetByRelationshipName($feature"RecordType2", ["Rep"], false);

if (count(relatedFeatures) > 0 && $feature.EventType == 'Sampling') {
    return text(count(relatedFeatures),"00");
else {
    return '00';
}
hope that is it
0 Kudos
StephenEJohnson
New Contributor

I stumbled across this idea that seems to work but I'm not sure I trust it. I think it relies on the fact that if this the current point is a new one it won't have a GlobalID?  I will try your ideas and see if they work. The logic in yours is easier to follow.

if ($feature.GlobalID != null) {
 var relatedFeatures = FeatureSetByRelationshipName($feature, "RecordType2", ["Rep"], false);
}else{
  return '00'
}

var relatedFeatures = FeatureSetByRelationshipName($feature, "RecordType2", ["Rep"], false);

if (!IsEmpty(relatedFeatures) && $feature.EventType == 'Sampling') {
    return text(count(relatedFeatures),"00");
} else {
    return '00';
}
0 Kudos
DougBrowning
MVP Esteemed Contributor

Oh yea new features.  In Attribute rules you can use context but I do not know if this works in Field Maps calculates.

if ($editContext.editType == "INSERT") {
    //code if the edit is an insert
} else if ($editContext.editType == "UPDATE") {
    //code if the edit is an update
} else if ($editContext.editType == "DELETE") {
    //code if the edit is a delete 
} else if ($editContext.editType == "NA") {
    //code when edit type is not applicable, for example batch calculation or validation rules
}

 

0 Kudos