I have a map layer that I am using to create a list of features in an ArcGIS Online dashboard. The list is set up to display some basic data from each feature, but I want to use Arcade to add an extra line that shows when the feature was last updated.
The last update timestamp is stored as a string in each feature ('YYYYMMDDHHMMSS') in a field called "LastGPSUpdate". I've written an Arcade Expression that works when I manually plug in a string, but whenever I try to reference the actual data, it returns null values. In fact, all of the "$datapoint.XYZ" globals return null values.
Any assistance would be much appreciated!
var dateInitial = $datapoint.LastGPSUpdate
var yearParse = Left(dateInitial, 4)
var monthParse = Right(Left(dateInitial, 6), 2)
var dayParse = Right(Left(dateInitial, 8), 2)
var dateParse = monthParse + '/' + dayParse + '/' + yearParse
Console(dateParse, $datapoint.BattalionArea,$datapoint.Beat,$datapoint.DispatchGroup,$datapoint.LastGPSUpdate,$datapoint.StationArea,$datapoint.Team,$datapoint.UnitType)
return {
textColor: '',
backgroundColor: '',
separatorColor:'',
selectionColor: '',
selectionTextColor: '',
dateText: dateParse
}
I'm not sure what input value you're testing this with, but you could try to convert the string to a true date value first, then use the Text function to format it.
And also, you can use Mid to get a set of characters from the middle of your text, rather than nesting right and left.
Additionally, you could check if the date string is null first, and return a default string.
var dateInitial = $datapoint.LastGPSUpdate
// stop expression early and return message if field is empty
if(IsEmpty(dateInitial)){
return 'Date string is empty!'
} else {
// convert to date
var dateval = Date(
Left(dateInitial, 4),
Mid(dateInitial, 4, 2),
Mid(dateInitial, 6, 2)
)
var dateParse = Text(dateval, 'MM/DD/YYYY')
return dateParse
}
Thanks for the tip about the Mid function, I haven't seen that one before.
It might just be my lack of understanding of Arcade in general, but I can't get any attribute to return a non-null value in the expression. I know that the data exists -- I can display it in the Line item template just fine, but for whatever reason, everything returns a null value when I try to use it in an expression.
return { textColor: '', backgroundColor: '', separatorColor:'', selectionColor: '', selectionTextColor: '', dateText: dateParse }
Are you calling it as {expression/dateText}? In your response you are using {expression/dateParse} but that is not correct going by the return statement you had above. To make things simple, I always name the attributes the same name as my variable:
dateParse: dateParse
Whoops, that was a typo in my example, thanks for catching that.
Unfortunately, changing to call it as {expression/dateParse} did not work. It is still returning null values in the console and no data is displayed in the list.