1

I'm a new developer who needs help with a SOQL query. As you know, the standard files and notes related lists on an Account do not include files/notes added on child objects (e.g., Opportunity, Contact, etc.). I have a requirement to create an LWC related list which does include the Account and child objects' files/notes.

I have created an apex class with queries that accomplish this requirement. However, I can't figure out who to include the linkedentityid in the query on the contentversion. I know there are limitations with querying the contentdocumentlink and displaying on the contentversion. Does anyone know how to include the linkedentityid? Basically, the request is to show the file/note and the record where it is related.

Thank you so much for the time and assistance!

LWC related list

public without sharing class FilesRelatedList {
    @AuraEnabled(cacheable = true)
    
    public Static  List<ContentVersion> cvRecords(String recId){

        //Create variable to hold Ids of Account and related Contacts, Asks, Tasks and Drive Opps
        set<Id> setIds = new Set<id>();

        //Retrive Ids of Account and related Contacts, Asks, Drive Opps and Tasks
            list<Account> lstAccount = [SELECT Id FROM Account WHERE Id = :recId];

            list<Contact> lstContact = [SELECT Id FROM Contact WHERE AccountId = :recId];

            list<Opportunity> lstOpportunity =[SELECT Id FROM Opportunity WHERE AccountId = :recId];

            list<Drive_Opportunity__c> lstDriveOpp =[SELECT Id FROM Drive_Opportunity__c WHERE Account__c = :recId];

            list<Task> lstTask = [SELECT Id, WhoId, WhatId FROM Task WHERE WhatId = :recId OR WhatId IN :lstOpportunity 
                                  OR WhatId IN :lstDriveOpp OR WhoId IN :lstContact];

        //Add the Ids from the above to the setIds variable
            for(Account acc : lstAccount){
                setIds.add(acc.Id);}
            for(Contact con : lstContact){
                setIds.add(con.Id);}
            for(Opportunity opp : lstOpportunity){
                setIds.add(opp.Id);}
            for(Drive_Opportunity__c driveopp : lstDriveOpp){
                setIds.add(driveopp.Id);}
            for(Task tsk : lstTask){
                setIds.add(tsk.Id);}

        
        //Retrieve all ContentDocumentLinks with a LinkedEntityId matching any Id in the setIds variable above
       List<ContentDocumentLink> files = [SELECT ContentDocumentId, LinkedEntityId, ContentDocument.LatestPublishedVersionId, 
            ContentDocument.FileExtension, ContentDocument.Title, ContentDocument.ContentModifiedDate, LinkedEntity.Name, 
            ContentDocument.OwnerId, ContentDocument.Owner.FirstName, ContentDocument.Owner.LastName
            FROM ContentDocumentLink WHERE LinkedEntityId = :setIds];

               
      
                return files;
    }
}
2
  • I'm unclear as to what you are asking .. the Related To column in your screen shot would be LinkedEntity.Name and you are retrieving that column
    – cropredy
    Apr 4 at 15:09
  • Thanks for your question. I am not able to include the LinkedEntityId or name in the contentversion SOQL query which is the query I'm using to populate the related list. I've tried to use dot notation to pull the LinkedEntityId into that query, but it's not allowed. That's where I'm stuck. I can only pull that field into the ContentDocumentLink query. Thanks.
    – Joe
    Apr 4 at 18:39

1 Answer 1

0

I think you have 2 main options here:

  1. instead of showing a list of ContentVersion, create a wrapper class in which you combine data from ContentDocumentLink and ContentVersion. Basically build a Map<Id,WrapperClass> where the Id is the ContentDocumentId. You then populate the records in the map while going through the results of the 2 queries.

  2. make use of the lesser-known 'LatestPublishedVersion' relationship on the ContentDocument object when querying the ContentDocumentLink. This relationship takes you directly to the latest ContentVersion record for a ContentDocument and it does allow the inclusion of relationship fields.

Example query (to be extended with all further fields you need):

SELECT  ContentDocument.LatestPublishedVersionId, ContentDocument.LatestPublishedVersion.FileExtension, ContentDocument.LatestPublishedVersion.Title
FROM ContentDocumentLink 
WHERE LinkedEntityId = :setIds

As far as I can assess, option #2 seems the more attractive and less complex one for this case.

8
  • Thank you for taking the time to explain those options. I will give it a try and let you know how it works. Thank you!
    – Joe
    Apr 4 at 20:46
  • Hi Guy, unfortunately, I'm not able to display a related list with the ContentDocumentLink object. I receive a " Illegal conversion from List<ContentDocument> to List<ContentVersion>" error even though I'm not trying to convert anything. It gives the same error if attempting to use the ContentDocumentLink. I guess I'll have to try to figure out that wrapper class. Thanks again for your assistance.
    – Joe
    Apr 5 at 19:14
  • what if you change the return type of your class? And on what line does it give that error? You should be able to get this to work. Apr 6 at 9:42
  • Good morning. When I try to return the ContentDocumentLink, I get the error that it's an illegal conversion from ID to Content version. //Create variable to hold the ContentDocumentIds and add all the ContentDocumentIds List<ID> fileIDs = new List<ID>(); for (ContentDocumentLink docLink : files) { fileIDs.add(docLink.ContentDocumentId); } return fileIDs; (error is Illegal conversion from List<Id> to List<ContentVersion>. If I try using ContentDocument, I get it too.
    – Joe
    Apr 6 at 13:55
  • This is definitely my ignorance in coding. I don't know how to change the return type or what too in this instance. Thanks.
    – Joe
    Apr 6 at 13:58

You must log in to answer this question.

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