Note: I’ve fixed an error in my latter examples for you, although that wasn’t, I think, the underlying cause of your failure to get the desired result.
Ah, I’d assumed from your starting comment that the children are all of the desired type. So now you need to also filter for notes of the appropriate type.
That can be fixed by:
if($ChildCount>0) {
$EndDate = collect_if(children,$Prototype=="TimeLine"&$EndDate,$EndDate).sort.($EndDate).at(-1);
}
Now the collect_if()
only collects and $EndDate for children that have both a prototype of “Timeline” and a non-default value for $EndDate.
Using .at(0)
won’t help as we’re sorting Dates in ascending order and .at(0)
is the first. also reversing the sort order (lest you try) also won’t help as the the underlying ‘problem’ is you need to ensure your list of dates contains no default "never"
items as these tend always sort where you don’t want them. IOW:
- Make sure the list of dates has no default values
- Then sort the list and take the last (
.at(-1)
or first (.at(0)
) as needed.
If still stuck, please upload a small test TBX that illustrates your problem. There’s no fault here but it is easy to overlook unmentioned factors that you are currently unaware have an effect of the outcome you desire by the manner described. For instance: the children of a Timeline container may varying prototypes—on none.
Looking at your replies above, another learning point. You starting changing things at the wrong ‘end’ of the chain, e.g. by altering the .at()
value. Given the computer adage “Garbage in, garbage out” the first test is to make sure you have a sensible output from collect_if()
. So, when the original solution failed, i would have used code like:
$MyList = collect_if(children,$EndDate,$EndDate);
and looked at the values. Aha, we see list mixing dates with “never”. So, our query needs to add extra term(s) to filter out those annoying “never” values †. So we make our test:
$MyList = collect_if(children,$Prototype=="never"&$EndDate,$EndDate);
Now $MyList, shows only actual dates. Now we can test the sort:
$MyList = collect_if(children,$Prototype=="never"&$EndDate,$EndDate).sort($EndDate);
The list should now be sorting in oldest to most recent dates. So now we can test catching just the last date:
$MyList = (collect_if(children,$Prototype=="never"&$EndDate,$EndDate).sort($EndDate)).at(-1);
Your list should now only hold 1 item, the latest of the dates. Note, if several source items have the same $EndDate the value chosen is the full-date-time value (as two $EndDates on the same daty but different times will sort so the one with the latest time comes last). But as you’re really only interested in the day of the last dates that nuance likely doesn’t matter.
Now your list has only one 9correct) value, we can use the code:
if($ChildCount) {
$EndDate = (collect_if(children,$Prototype=="never"&$EndDate,$EndDate).sort($EndDate)).at(-1);
};
If still stuck, do consider posting a TBX showing the problem with attribute values such as you are actually using.
†. Before you ask there isn’t a pre-build action code to filter lists of dates to remove non-date values. You could write a function for that, but it turns out it is easier just to not collect them in the first place (as shown above)