1

Is there any way (Besides iterating through the list and populating a new one) to access the fields of every object in a list (set or even a map as this applies to every collection) as a list?.

Take the following example, i'm looking for a way somewhat similar to this:

List<Account> myAccList = new List<Account>([select HelloStackOverflow__c from Account]);
List<Case> myCaseList = new List<Case>([select Id from Case where CanBeDone__c in :myAccList.HelloStackOverflow__c]);

It would save some time while making the code less cluttered and more readable.

2
2

If you'll be doing this often, it's probably worth it to create a Utility function to use in obtaining information from sObject fields. It might not save you a loop, but it will make your code look much nicer.

public class Utility {

    public static List<String> getStringFieldFromList(List<sObject> records, String field) {
        List<String> result = new List<String>();

        for (sObject record:records) {
            // Assumes excluding null/empty values
            if (!Utility.isBlank(record.get(field))) {
                result.add(record.get(field));
            }
        }

        return result;
    }

    public static Boolean isBlank(String s) {
        if (s == null || s == '') {
            return true;
        }

        return false;
    }

}

Using the function will make your code look something like this:

List<Account> myAccList = new List<Account>([select HelloStackOverflow__c from Account]);
List<Case> myCaseList = new List<Case>([select Id from Case where CanBeDone__c in :Utility.getStringFieldFromList(myAccList, 'HelloStackOverflow__c')]);

Note that I specify both the field type in the function name (getStringFieldFromList(...)), and use List<String> as a return type. This to avoid some of the complexities that come with returning a List<Object>, and trying to cast that list back into what type it was before it was returned, as well as determining what type of data the field stores.

If you want to make it more complex, you could also create your own list type, by inheriting Iterable and creating a number of methods to act similarly to the default list type. That object would have an internal list, and a number of functions to obtain types, get field information, and other necessities. Its not something to do lightly, as it increases overhead, and from my experience, was complex to set up, but it is an option.

I prefer object creation, as it makes re-using the same list easier, and is very clear once you have all the code written (and written well), but for a short term solution, the static method is a solid call.

1
  • This worked great, I will have to make some performance tests to see how it behaves in heavy load scenarios though. Props also to @cropredy for posting something very similar to your solution in the meta. – FuuRe Aug 11 '16 at 13:51

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.