0

I have a SOQL query which is returning a list of documents.

I want to be able to separate the list of docs depending on who the author is, and display the different lists in 3 different data tables on the same component.

I want to query the database just once.

Can someone point me in the right direction to separate the SOQL results?

Just need a document or link that i can work through.

4
  • 2
    I don't think there's going to be any documentation that specifically talks about what you're looking to do. In the end, it almost always boils down to iterating over collections. What, specifically, are you stuck on?
    – Derek F
    Oct 14, 2019 at 12:40
  • Feels like some form of ORDER BY is going to help with this, and iteration that looks for the "split" conditions when iterating the collection.
    – Phil W
    Oct 14, 2019 at 12:48
  • Hi Derek F, I'm new to this, and keep losing my way. So I feel like I'm stuck on it all. But to be more specific, I have a working SOQL query that is returning the necessary results. I also have those results being returned to a single lightning:datatable as one list. Where I struggle is what method to use to separate them out. Should I be using loops or if statements? And should I be doing it on the server end, or on the client end? The options seem limitless to me, and I struggle to find the starting point as a result.
    – danmwendo
    Oct 14, 2019 at 13:03
  • You're going to need both loops and conditionals. I'm not very familiar with aura bundles, and a little familiar with LWC, but it seems to me like you should be able to do this on either the client or server side. The discussion about which one is better is likely subjective and changes with trends in development. It sounds to me like you have some idea of what you'd need to do. My advice would be to pick one and see where it takes you. If you need a bit of a push, try doing it in apex and store things in a Map<String, List<Document>> (the key of the map being the name of the author).
    – Derek F
    Oct 14, 2019 at 13:48

1 Answer 1

3

You can just use a Map to do this, perhaps like this:

Map<Id, ContentDocument[]> docsByAuthor = new Map<Id, ContentDocument[]>();
for(ContentDocument doc: [SELECT CreatedById FROM ContentDocument]) {
  ContentDocument[] docs = docsByAuthor.get(doc.CreatedById);
  if(docs == null) {
    docsByAuthor.put(doc.CreatedById, docs = new ContentDocument[0]);
  }
  docs.add(doc);
}

The result of this code would be a Map where the Key is the CreatedById for each group of documents created by the same person, and the value a list of their documents. You'll see some variants of this code out on the Internet, such as:

Map<Id, ContentDocument[]> docsByAuthor = new Map<Id, ContentDocument[]>();
for(ContentDocument doc: [SELECT CreatedById FROM ContentDocument]) {
  if(!docs.containsKey(doc.CreatedById)) {
    docsByAuthor.put(doc.CreatedById, new ContentDocument[0]);
  }
  docsByAuthor.get(doc.CreatedById).add(doc);
}

Or:

Map<Id, ContentDocument[]> docsByAuthor = new Map<Id, ContentDocument[]>();
for(ContentDocument doc: [SELECT CreatedById FROM ContentDocument]) {
  if(docs.containsKey(doc.CreatedById)) {
    docsByAuthor.get(doc.CreatedById).add(doc);
  } else {
    docsByAuthor.put(doc.CreatedById, new List<ContentDocument>{ doc }); 
  }
}

etc.

I prefer my version because it appears to have the best benchmark of all variants I've found and tested, but do feel free to use whichever you feel most comfortable with and understand, as the performance difference is usually pretty small.

2
  • Hi SFDCFOX, thanks so much. I'm now struggling to traverse the payload in my lightning component. I can see the payload is there in the console, but other than that I can't seem to access the data. Have you got any tips on that?
    – danmwendo
    Nov 1, 2019 at 13:27
  • @danmwendo We'd have to see your code for detailed help, but generally, the data will look like {'user1':[{name:'record1',...},...],'user2':[...],...}. So, you'll need to convert this to whatever format matches your template/markup. You might want to use Object.keys and Array.map to help parse the data appropriately.
    – sfdcfox
    Nov 1, 2019 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .