CanTheUser fails if empty collection is passed
dschach opened this issue · comments
I don't have a quick solution, but it seems that crud(objs?.get(0)
returns an error System.ListException: List index out of bounds: 0
when passed an empty collection.
I found a reference at https://www.salesforcepoint.com/2020/08/safe-navigation-operator-in-apex-salesforce.html but it doesn't seem to be true.
The operator evaluates to its left and then to its right. So it evaluates if objs
is null. If there's no operator to the right of get(0)
, then it returns get(0)
. But get(0)
doesn't exist, so we have an error.
This test fails:
@IsTest static void emptyCollection(){ System.assert(CanTheUser.destroy(new List<Account>())); }
The problem is that I don't want to use Safely and have it return true if we pass an empty list. It's almost like we need to call CanTheUser if the list has an item, but if the list doesn't have an item, we should just exit the Safely method.
I'm submitting a PR for just that - feel free to reject it if you think there's a better way.