5

Is there a guarantee documented anywhere that the ordering of the SObjectAccessDescision's getRecords() method will match the ordering of the input list?

It seems to be the behavior but I can't see any explicit documentation concerning the ordering.

List<Account> accs = new List<Account>();
for (String name : new List<String> { 'a', 'b', 'c', 'd', 'e' }) {
    Account a = new Account(Name = name);
}

// passing true as the third parameter guarantees the length of the returned list is equal
// to the input list (otherwise exception is thrown) but no guarantee on the ordering?
List<Account> strippedAccounts = Security.stripInaccessible(System.AccessType.UPDATABLE, accs, true).getRecords();
for (Integer i = 0; i < strippedAccounts.size(); i++) {
    System.assertEquals(strippedAccounts[i].Name, accs[i].Name);
}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_SObjectAccessDecision.htm#topic-title

getRecords()

Returns a list of new sObjects that are identical to the source records, except that they are stripped of fields that fail the field-level security check for the current user.

Can't tell if 'identical' applies to individual records or the list as a whole.

4

The documentation is never as explicit as this (unfortunately). However, it is nevertheless true. The ordering of the records in the list will be preserved. This is also true for other methods that return lists of objects that don't appear to guarantee order (e.g. Database.insert will return SaveResult objects in the same order as the original DML list).

| improve this answer | |
  • 1
    The documentation should be as explicit as this, and indeed is for handling of DML SaveResults vs the input list, for example. This is a significant oversight in the documentation for this new feature. – Phil W Mar 10 at 16:23
  • 1
    This is great to know. Having the ordering preserved is essential especially during an insert operation. Otherwise there's no way to set the ID field on the unstripped copy of the records. – bafuda Mar 10 at 16:44
  • @PhilW Oh, that looks relatively new... or maybe I'm just misremembering. Either way, good to see that something like that does somewhere, I have something to reference in the future. – sfdcfox Mar 10 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.