Select to view content in your preferred language

"else" text not appearing in arcade expression

597
4
05-10-2023 07:35 AM
Labels (1)
ScottStepro1
New Contributor III
Howdy all, new to Arcade and having a bit of an issue returning text.  The expression runs without errors however if there is no record, it does not return "None to Date" as I expected it would... 
 
any help is greatly appreciated (having issues with datediff returns but that'll be a different topic!)
 
Thanks in advance!
 
expression as currently written
 
var relRec = OrderBy(FeatureSetByRelationshipName($feature , "T4C_Records"), "CollectDate DES")
var cnt = Count(relRec)
var relatedInfo = ""

 

if (cnt>0) { var info = First(relRec);
    relatedInfo = Text(ToLocal(info.CollectDate),"MM/DD/Y");
} else var relatedInfo="None To Date";

 

return relatedInfo;
0 Kudos
4 Replies
JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1677736512957.png
JohannesLindner_1-1677736529803.png

 

Can't see anything obviously wrong at first glance. Maybe you see features that do have related records, but those records have null values in the date field? In that case, the expression returns an empty string.

Try to incorporate that condition:

var relRec = OrderBy(FeatureSetByRelationshipName($feature , "T4C_Records"), "CollectDate DES")
var info = First(relRec)

return IIf(info == null || info.CollectDate == null,
    "None To Date",
    Text(ToLocal(info.CollectDate), "MM/DD/Y")
)

 


Have a great day!
Johannes
0 Kudos
ScottStepro1
New Contributor III

Hi Johannes, thanks for the reply!

I tried your example and it returned a date when they exist, however it still did not return "None To Date" for null values.  Basically, this returns just the date... I tried using the else to return the null statement without success.  Not sure how to expand this expression to do that...

var recOrder = OrderBy(FeatureSetByRelationshipName($feature, "T4C_Records"),"CollectDate DES" );
var countRec = count(recOrder);
var relatedInfo = "";
if (countRec>0) { var info = First(recorder);
relatedInfo = Text(ToLocal(info.CollectDate), "MM/DD/Y");}
return relatedInfo

I got the expression from here Pulling Related Records with Arcade but it did not expand beyond the date pull.

 

Thanks!

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Maybe if you set the return value to the default and only edit it if all the conditions are met?

var recOrder = OrderBy(FeatureSetByRelationshipName($feature, "T4C_Records"),"CollectDate DES" );
var countRec = count(recOrder);

var relatedInfo = "None to date";
if (countRec > 0) {
  var lastDate = First(recOrder).CollectDate;
  if(lastDate != null) {
    relatedInfo = Text(ToLocal(lastDate), "MM/DD/Y");
  }
}
return relatedInfo

 

 

You could also filter out null dates:

var rec = FeatureSetByRelationshipName($feature, "T4C_Records")
var recFilter = Filter(rec, "CollectDate IS NOT NULL")
var recOrder = OrderBy(recFilter, "CollectDate DES");
var countRec = count(recOrder);

var relatedInfo = "None to date";
if (countRec>0) {
  var info = First(recOrder);
  relatedInfo = Text(ToLocal(info.CollectDate), "MM/DD/Y");
}
return relatedInfo

Have a great day!
Johannes
0 Kudos
ScottStepro1
New Contributor III

Still not working, those both do the same thing as the previous versions...  seems like it should be fairly cut and dry but I'm scratching my head for sure

0 Kudos